10

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

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>在代码中包含了所需的标题。有人可以帮我吗?

4

3 回答 3

15

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

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

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

#include <iterator>

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

于 2013-11-09T15:28:45.587 回答
2

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

int source[10];
int dest[10];

std::copy(std::begin(source), std::end(source), std::begin(dest));
于 2013-11-09T15:34:56.807 回答
-1

在linux中使用g++编译器这个代码时也有这个问题。

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

g++ -std=c++11 -o test test.cpp
于 2018-04-04T01:54:23.923 回答