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

I am trying to perform 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 someone help me with this?

+7
c ++ iterator std compiler-errors syntax-error
source share
2 answers

The template functions std :: begin () and std :: end () are not implemented for pointers (pointers do not contain information about the number of elements to which they refer) Instead you must write

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

As for the error, you should check if the header is enabled

 #include <iterator> 

Your compiler may not support the C ++ 2011 standard.

+12
source share

In addition to include <iterator> in a compiler with C ++ 11 support, you should know that begin/end not useful for pointers, they are useful for arrays:

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

All Articles