copy part of vector 1 to vector 2
Hi,
I am trying to copy vector to vector as follow:
Code:
std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;
//now i wanto to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?
std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());
Re: copy part of vector 1 to vector 2
Quote:
Originally Posted by mce
why do i need to know how many bytes from the end of vec1?
You don't, but you do need to know that vec1 has the elements that you want to copy.
Quote:
Originally Posted by mce
can't i just specify how many bytes i want to copy from starting index?
Yes, e.g.,
Code:
std::vector<unsigned char> vec1;
// insert some values into vec1
// ...
// copy 2 bytes from vec1 starting at index 5.
std::vector<unsigned char> vec2(vec1.begin() + 5, vec1.begin() + 7);
Same idea applies for std::copy, except that you will need pass std::back_inserter(vec2) as the third argument, otherwise you will be copying to elements that don't exist.
Re: copy part of vector 1 to vector 2
TQ so much, don know why my mind just got stuck when i was shown the example of copy using begin() and end()..... sighhhhhhhhh