使用参考。std::generate 的行为应该等同于:
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
while (first != last) {
*first = gen();
++first;
}
}
但是当我尝试用 0-99 数字填充矩阵时。相反,我得到 10 行 0-9。这是为什么?
std::vector< std::vector<int> > m;
#ifdef GENERATE
struct Generator{
int seq;
Generator():seq(0){};
int operator()(){return seq++;};
} generator;
#elif defined FOR_EACH
int i = 0;
auto increment = [&i](int &a){a = i++;};
#endif
int x = 10, y = 10;
m.resize(x);
for(auto &v : m)
{
v.resize(y);
#ifdef GENERATE
generate(begin(v), end(v), generator);
#elif defined FOR_EACH
for_each(begin(v),end(v), increment);
#endif
}