C++ 11:错误:“开始”不是“性病”

问题描述:

我尝试做以下操作中的一员:C++ 11:错误:“开始”不是“性病”

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>标题。有人可以帮助我吗?

+3

启用C++ 11。 (fill) – 0x499602D2

模板功能标准::开始()和std ::结束()不是为指针实施(指针不包含有关它们指的是元素的数量信息)相反,他们应该写

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

至于错误,你应该检查是否包含的头

#include <iterator> 

而且也许你的编译器不支持C++ 2011标准。

+0

如果你要定义source和dest为int source [10],dest [10];那么你的确可以使用这些功能。 –

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

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

std::copy(std::begin(source), std::end(source), std::begin(dest)); 
+0

+1但是,如果他有权访问C++ 11功能,他应该使用'std :: array'。 – 0x499602D2

+1

@ 0x499602D2:同意,但有时一个简单的'[]'对于简单的项目/代码来说不是一个错误的选择。 – deepmax