1 template<classclass outputiterator>2 outputiterator Copy ( Inputiterator First, inputiterator, outputiterator result)3{4while (first!= last) {5 *result = * First; 6 ++result; + + first; 7 }8 return result;
The copy function is equivalent to the above code, and is not implemented when it is first used, on the code
1 intMain ()2 {3vector<int> v = {1,2,3};4vector<int>CV;5 copy (V.begin (), V.end (), Cv.begin ());6 for(auto A:CV)7cout<<A;8 return 0;9}
According to the function template found, *result = *first; ++result;
The reason is that the CV is now an empty container, so it is not possible to assign a value, run the problem of naturally occurring array out of bounds.
1 intMain ()2 {3vector<int> v = {1,2,3};4vector<int> CV (3) ;5 copy (V.begin (), V.end (), Cv.begin ());6 for(auto A:CV)7cout<<A;8 return 0;9}
When defining, allocate space for CV
C + + vector copy function