C++11 : error: ‘begin’ is not a member of ‘std’
Solution 1
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::copy( source, source + 10, dest);
As for the error you should check whether you included header
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
Solution 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:
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
SKPS
Stack Overflow posting etiquette: Stack Overflow - Minimal reproducible example Interesting matplotlib/pandas references (for myself :P) Matplotlib animations Pivoting examples in Pandas
Updated on June 23, 2022Comments
-
SKPS almost 2 years
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? -
Vlad from Moscow over 10 yearsIf you would define source and dest as int source[10], dest[10]; then indeed you could use these functions.
-
David G over 10 years+1 But if he has access to C++11 features he should be using
std::array
. -
masoud over 10 years@0x499602D2: Agree, but sometimes a simple
[]
is not a bad choice for simple projects/codes.