I am trying to do the following operation:
我正在做以下操作:
source = new int[10];
dest = new int[10];
std::copy( std::begin(source), std::end(source), std::begin(dest));
However, the compiler reports the following error.
但是,编译器报告如下错误。
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’
I have included the required <iterator>
header in the code. Can anybody help me on this?
我在代码中包含了所需的
3 个解决方案
#1
13
Template functions std::begin() and std::end() are not implemented for pointers (pointers do not contain information about the number of elements they refer to) Instead them you should write
模板函数std::begin()和std: end()不是为指针实现的(指针不包含它们引用的元素数量的信息),而是您应该编写的
std::copy( source, source + 10, dest);
As for the error you should check whether you included header
至于错误,您应该检查是否包含了header。
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
也许你的编译器不支持c++ 2011标准。
#2
2
In addition to include <iterator>
in C++11 enabled compiler. You should know begin/end
are not useful for pointers, they're useful for arrays:
此外,在c++ 11启用的编译器中还包括
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
#3
-1
also have this problem when using g++ compiler this code in linux.
在linux中使用g++编译器时也有这个问题。
Using g++ compiler that contain C++ featuer should add C++11 flag
使用包含c++特性的g++编译器应该添加c++ 11标志
g++ -std=c++11 -o test test.cpp
#1
13
Template functions std::begin() and std::end() are not implemented for pointers (pointers do not contain information about the number of elements they refer to) Instead them you should write
模板函数std::begin()和std: end()不是为指针实现的(指针不包含它们引用的元素数量的信息),而是您应该编写的
std::copy( source, source + 10, dest);
As for the error you should check whether you included header
至于错误,您应该检查是否包含了header。
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
也许你的编译器不支持c++ 2011标准。
#2
2
In addition to include <iterator>
in C++11 enabled compiler. You should know begin/end
are not useful for pointers, they're useful for arrays:
此外,在c++ 11启用的编译器中还包括
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
#3
-1
also have this problem when using g++ compiler this code in linux.
在linux中使用g++编译器时也有这个问题。
Using g++ compiler that contain C++ featuer should add C++11 flag
使用包含c++特性的g++编译器应该添加c++ 11标志
g++ -std=c++11 -o test test.cpp