我有一小段std::for_each_n
循环代码.我尝试使用以下命令在内置的Coliru编译器GCC C++ 17 上运行它:
g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
但编译器会给出错误"'for_each_n'不是'std'的成员".
我的代码是从cppreference复制的下面的代码.
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> ns{1, 2, 3, 4, 5};
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
std::for_each_n(ns.begin(), 3, [](auto& n){ n *= 2; });
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
那么,为什么我收到错误?
归档时间: |
|
查看次数: |
1013 次 |
最近记录: |