I got a list and a vector:
list<int> l;
for(int i=0; i<10; i++)
l.push_back(i);
vector<int> v;
I want to initialize v using the first 5 elements in l, that is in this case {0,1,2,3,4}.
Besides for loop coupled with v.push_back, any other way? memcpy or copy?
PS: I am not using C++0x/C++11 at present.
You can use
std::copyandstd::advance(instead of+, for the non-random-access iterator):The
advanceis “slow”, though, because of the nature ofstd::list<>; that’s whyop+is not available for thestd::list<>::iterator.