gpt4 book ai didi

c++ - 'for_each_n'不是C++ 17中 'std'的成员

转载 作者:行者123 更新时间:2023-12-01 14:58:14 25 4
gpt4 key购买 nike

我有std::for_each_n循环的一小段代码。我尝试使用以下命令在内置Coliru编译器GCC C++ 17上运行它:

g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out

但是编译器给出了一个错误,即 “'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';
}

那么,为什么我会出错?

最佳答案

您的代码没有错。问题是libstdc++到目前为止还不支持std::for_each_n。如果我们查看定义std::for_eachheader,我们将发现它不存在。

但是,如果您可以访问libc++,则official mirror的 header 确实实现了std::for_each_n

关于c++ - 'for_each_n'不是C++ 17中 'std'的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com