我正在尝试填充一个随机生成的数字的向量,但问题是当我运行它时,排序后我只得到 2 个数字。
0 0 0 0 0 17 17 17
这是我的代码,所以你能告诉我我做错了什么吗
#include <iostream>
#include <random>
#include <functional>
using namespace std;
using namespace std::chrono;
class Rand_int {
public:
Rand_int(int low, int high) :dist{low,high} {
for (int i = 0; i < 100; i++)
cout << "Generated: " << dist(re) << endl;
num = dist(re); }
int operator()() { return dist(re);}
int getNum() const
{
return num;
}
bool operator()(int x) const
{
return (num <= x);
}
private:
default_random_engine re;
uniform_int_distribution<> dist;
int num;
};
int main()
{
int n=10;
vector<int> v(n);
Rand_int rnd {0,20}; // make a uniform random number generator
for (int i=0; i<n; i++)
{
vector<int>::iterator iter = find_if(v.begin(), v.end(), rnd);
v.insert(iter, rnd.getNum());
}
for (vector<int>::const_iterator iter = v.begin(); iter != v.end(); iter++)
cout << *iter << endl;
return 0;
}