来自 Python
世界,我找到了函数 std::iota
非常有限。为什么接口(interface)被限制为不带任何 UnaryFunction
?
例如我可以转换
>>> x = range(0, 10)
进入
std::vector<int> x(10);
std::iota(std::begin(x), std::end(x), 0);
但是怎么办呢:
>>> x = range(0,20,2)
甚至
>>> x = range(10,0,-1)
我知道编写一个这样的函数或使用 Boost 是微不足道的,但我认为 C++ 委员会必须谨慎选择这种设计。很明显,我在 C++11 中遗漏了一些东西。
最佳答案
std::generate
怎么样| ?
int n = -2;
std::generate(x.begin(), x.end(), [&n]{ return n+=2; });
int n = 10;
std::generate(x.begin(), x.end(), [&n]{ return n--;});
关于c++ - std::iota 非常有限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34544665/