CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    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());

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    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
  •  





Click Here to Expand Forum to Full Width

Featured