-
June 21st, 2012, 10:10 PM
#1
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());
-
June 21st, 2012, 10:33 PM
#2
Re: copy part of vector 1 to vector 2
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.
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.
-
June 21st, 2012, 10:40 PM
#3
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|