'for_each_n'在C ++ 17中不是'std'的成员 [英] 'for_each_n' is not a member of 'std' in C++17

查看:232
本文介绍了'for_each_n'在C ++ 17中不是'std'的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是编译器给出一个错误,即'for_each_n'不是'std'的成员。

But compiler give an error that " 'for_each_n' is not a member of 'std' ".

我的代码在下面,是从 cppreference

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?

推荐答案

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

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.

但是,如果有访问libc ++,它们的官方镜像的标头确实实现了 std :: for_each_n

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

这篇关于'for_each_n'在C ++ 17中不是'std'的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆