简体   繁体   English

“for_each_n”不是 C++17 中“std”的成员

[英]'for_each_n' is not a member of 'std' in C++17

I have small piece of code for std::for_each_n loop.我有一小段用于std::for_each_n循环的代码。 I tried running it on inbuilt Coliru compiler GCC C++17 using following command :我尝试使用以下命令在内置Coliru编译器 GCC C++17 上运行它:

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

But compiler give an error that " 'for_each_n' is not a member of 'std' ".但是编译器给出了一个错误, “'for_each_n' 不是 'std' 的成员”。

My code is bellow which is copied from cppreference .我的代码如下,是从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';
}

So, Why I'm getting an error?那么,为什么我会收到错误消息?

There is nothing wrong with your code.您的代码没有任何问题。 The issue is that libstdc++ does not support std::for_each_n as of yet.问题是 libstdc++ 目前还不支持std::for_each_n If we look at header that defines std::for_each we see it does not exist.如果我们查看定义std::for_each 头文件,我们会发现它不存在。

However, if you have access to libc++, their header from the official mirror does implement std::for_each_n但是,如果您可以访问 libc++,那么官方镜像中的标头确实实现了std::for_each_n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM