下面的代码用于生成区间[1,100]中的五个伪随机数的列表.我播种default_random_engine
with time(0)
,它在unix时间内返回系统时间.当我使用Microsoft Visual Studio 2013在Windows 7上编译和运行此程序时,它按预期工作(见下文).但是,当我使用g ++编译器在Arch Linux中这样做时,它表现得很奇怪.
在Linux中,每次都会生成5个数字.最后4个数字在每次执行时都会有所不同(通常情况就是如此),但第一个数字将保持不变.
Windows和Linux上5次执行的示例输出:
| Windows: | Linux:
---------------------------------------
Run 1 | 54,01,91,73,68 | 25,38,40,42,21
Run 2 | 46,24,16,93,82 | 25,78,66,80,81
Run 3 | 86,36,33,63,05 | 25,17,93,17,40
Run 4 | 75,79,66,23,84 | 25,70,95,01,54
Run 5 | 64,36,32,44,85 | 25,09,22,38,13
Run Code Online (Sandbox Code Playgroud)
除此之外,第一个数字在Linux上定期递增1.在获得上述输出后,我等了大约30分钟并再次尝试发现第一个数字已经改变,现在总是以26为单位生成.它继续定期增加1,现在是32.它似乎对应随着价值的变化time(0)
.
为什么第一个数字很少在运行中发生变化,然后在运行时,增加1?
代码.它整齐地打印出5个数字和系统时间:
#include <iostream>
#include <random>
#include <time.h>
using namespace std;
int main()
{
const int upper_bound = 100;
const int lower_bound = 1;
time_t …
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
public void ReadSomeFile(string filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException();
var stream = new FileStream(filePath, ....)
.....
}
Run Code Online (Sandbox Code Playgroud)
我应该自己抛出异常(见File.Exists
支票)吗?如果文件不存在,FileStream
则会抛出FileNotFoundException
.这里有什么好的编程习惯?代码分析说我们应该验证我们的参数.但是如果我将该参数直接传递给另一个方法(我的或其他代码)并且该方法本身会抛出异常,那么在我的代码中验证参数的优点是什么?
typedef map<string, string> myMap;
Run Code Online (Sandbox Code Playgroud)
插入新对时myMap
,它将使用键string
通过自己的字符串比较器进行比较.是否可以覆盖该比较器?例如,我想比较密钥string
的长度,而不是字母表.或者还有其他方法可以对地图进行排序吗?
考虑一下这段代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyStruct
{
int key;
std::string stringValue;
MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
bool operator < (const MyStruct& other) {
return (key < other.key);
}
};
int main() {
std::vector < MyStruct > vec;
vec.push_back(MyStruct(2, "is"));
vec.push_back(MyStruct(1, "this"));
vec.push_back(MyStruct(4, "test"));
vec.push_back(MyStruct(3, "a"));
std::sort(vec.begin(), vec.end());
for (const MyStruct& a : vec) {
cout << a.key << ": " << a.stringValue << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好并且给出了预期的输出.但是,如果我尝试按降序排序结构:
#include …
Run Code Online (Sandbox Code Playgroud) 我不知道std::atomic
变量,但是知道std::mutex
标准提供的(奇怪的权利!); 然而有一件事引起了我的注意:标准提供了两种看似相同(对我而言)的原子类型,如下所示:
它也以 - 的例子提到std::atomic_flag type
-
std :: atomic_flag是一种原子布尔类型.与std :: atomic的所有特化不同,它保证是无锁的.与std :: atomic不同,std :: atomic_flag不提供加载或存储操作.
我不明白.std::atomic bool type
不保证是无锁的吗?那它不是原子的还是什么?
那么两者之间有什么区别呢?我应该在何时使用哪种?
我正在尝试从bitbucket克隆我的git repo,当我输入此命令时:
git clone https://naor_shoyhat@bitbucket.org/naor_shoyhat/hello-world.git
Run Code Online (Sandbox Code Playgroud)
bash然后提示我一个窗口,其中包含输入用户名和密码的区域.
我输入它们然后我收到此错误:
remote: Empty password
fatal: Authentication failed for 'https://naor_shoyhat@bitbucket.org/naor_shoyhat/hello-world.git/'
Run Code Online (Sandbox Code Playgroud) 如何在C中创建一个可以作为"按任意键继续"的空函数?
我想做的是:
printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
//The Void Function Here
//Then I will call the function that will start the game
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2012进行编译.
std::filesystem
在C++ 17上,std::experimental::filesystem
对于许多前C++ 17编译器来说,基于boost::filesystem
并且几乎所有它都很明显地移植到较新的std.
但我认为不std::filesystem
相同boost::filesystem::unique_path()
.
我没有注意到std中的等价物吗?或者我是否应该采用一种推荐的方法来模仿实现?
boost::filesystem
当我的代码注意到它在支持的平台上进行编译时,我真的希望能够替换依赖std::filesystem
,并且unique_path()
是我转换中唯一不明显的部分.
我想为我的用户文档创建一个带有Mongoose的模型层,它可以:
在持久化到db之前,需要执行所有这些操作.幸运的是,mongoose支持验证,插件和中间件.
不好的是我找不到关于这个问题的任何好材料.mongoosejs.com上的官方文档太短了......
有没有人有一个关于Mongoose预处理的例子(或一个完整的插件,如果它存在的话)?
问候
vector<int>
有两个vector<int>::size_type
和vector<int>::difference_type
.两者似乎都不必存在,因为size_type
保证能够保存与vector<int>
给定系统上可能包含的最大元素数一样大的值,并且在任何有效用例中,difference_type
都必须小于或等于元素的最大数量,即顺序容器中两个元素之间的距离绝不会大于顺序容器可以包含的最大元素数.有人能提供一个例子,两者之间有什么区别吗?