C++11 : 错误:“开始”不是“std”的成员

C++11 : error: ‘begin’ is not a member of ‘std’

本文关键字:成员 std 开始 错误 C++11 不是      更新时间:2023-10-16

我正在尝试执行以下操作:

source = new int[10];
dest =  new int[10];
std::copy( std::begin(source), std::end(source), std::begin(dest));

但是,编译器报告以下错误。

copy.cpp:5434:14: error: ‘begin’ is not a member of ‘std’
copy.cpp:5434:44: error: ‘end’ is not a member of ‘std’
copy.cpp:5434:72: error: ‘begin’ is not a member of ‘std’

我已经在代码中包含所需的<iterator>标头。有人可以帮助我吗?

模板函数 std::begin() 和 std::end() 不是为指针实现的(指针不包含有关它们引用的元素数量的信息) 相反,你应该写

std::copy( source, source + 10, dest);

至于错误,您应该检查是否包含标题

#include <iterator>

另外,您的编译器可能不支持C++ 2011标准。

除了在启用C++11的编译器中包含<iterator>。你应该知道begin/end对指针没有用,它们对数组有用:

int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));

在Linux中使用G ++编译器此代码时也会遇到此问题。

使用包含C++特征的 g++ 编译器应添加 C++11 标志

g++ -std=c++11 -o test test.cpp