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

柔情痞子 提交于 2020-01-29 17:40:43

问题


I have small piece of code for std::for_each_n loop. I tried running it on inbuilt Coliru compiler GCC C++17 using following command :

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' ".

My code is bellow which is copied from 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?


回答1:


There is nothing wrong with your code. The issue is that libstdc++ does not support std::for_each_n as of yet. If we look at header that defines std::for_each we see it does not exist.

However, if you have access to libc++, their header from the official mirror does implement std::for_each_n



来源:https://stackoverflow.com/questions/46468114/for-each-n-is-not-a-member-of-std-in-c17

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!