c++ - Cannot use the copy function -
what wrong copy
(which generic function), here? cannot run code.
vector<int> a(10, 2); vector<int> b(a.size()); auto ret = copy(a.begin(), a.end(), b); (auto : b) cout << << endl;
here's output after compiling:
1>------ build started: project: project1, configuration: debug win32 ------ 1> mainex.cpp 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2176): error c4996: 'std::_copy_impl': function call parameters may unsafe - call relies on caller check passed values correct. disable warning, use -d_scl_secure_no_warnings. see documentation on how use visual c++ 'checked iterators' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2157) : see declaration of 'std::_copy_impl' 1> c:\users\amin\documents\visual studio 2012\projects\project1\project1\mainex.cpp(40) : see reference function template instantiation '_outit std::copy<std::_vector_iterator<_myvec>,std::vector<_ty>>(_init,_init,_outit)' being compiled 1> 1> [ 1> _outit=std::vector<int>, 1> _myvec=std::_vector_val<std::_simple_types<int>>, 1> _ty=int, 1> _init=std::_vector_iterator<std::_vector_val<std::_simple_types<int>>> 1> ] ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
std::copy 3rd parameter iterator, pass b.begin()
instead:
#include <iterator> ... auto ret = std::copy(a.begin(), a.end(), b.begin());
the better way construct b
a
, in case, compiler knows allocate needed memory @ once , construct elements a
:
vector<int> b(a.begin(), a.end());
or
std::vector<int> b = a;
Comments
Post a Comment