Thread: copying vector to list

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    copying vector to list

    Hello

    I have a std::vector<std::string> with some elements..
    I would like to copy last X elements to another std::list<std::string> and remove them from std::vector.

    What is the easiest/best way to do this?

    I know I should use std::copy and do interation, but not sure about the second step.

    Thanks a lot for help!

    Regards

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    iterators would work well I think.
    Code:
    std::vector<std::string> myvector;
    std::list<std::string> mylist;
    
    std::vector<std::string>::iterator iter;
    for ( iter = myvector.begin(); iter != myvector.end(); ++iter )
    {
      mylist.push_back((*iter));
     myvector.erase(iter);
    }
    that will copy the entire vector, and erase each element as it goes. you could modify that to take only certain elements.
    just put + however many elements you want to skip after the .begin()
    Code:
    iter = myvector.begin()+SkipAmount;
    Hopefully that helped.

    EDIT:
    If your wanted to always only take so many elements, then maybe a reverse_iterator would work better.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for reply but arent there any simpler/better methods (maybe like):

    std::copy(vector.begin() + 10, vector.end(), ..);
    std::del(vector.begin() + 10, vector.end(), ..);

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yea. I am a little weird though. I like writing my own solutions for such problems. Either way you do it will be about the same amount of code.
    might try looking at the reference on http://www.cplusplus.com
    The reference there is thorough, and explains things well.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    std::copy(vec.begin(), vec.end(), std::back_inserter(list));
    vec.clear();

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use the erase-remove idiom for the 2nd step if you only have access to vector iterators; if you have access to the vector object itself, use the vector.erase() function.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Use the erase-remove idiom for the 2nd step.
    I prefer to assume the container will be reused some time in the near future -- unless it's possible to show otherwise. If it will be reused, no point releasing its storage. If it will not be reused, I question why it does not immediately go out of scope at that point.

    EDIT: Nevermind that whole thing. I was looking at the swap-trick part of the page you linked. Sorry.
    Last edited by brewbuck; 03-31-2008 at 09:05 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    I prefer to assume the container will be reused some time in the near future -- unless it's possible to show otherwise. If it will be reused, no point releasing its storage. If it will not be reused, I question why it does not immediately go out of scope at that point.
    I think l2u needed to remove some of the vector, not all of it.
    I would like to copy last X elements to another std::list<std::string> and remove them from std::vector.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by cpjust View Post
    I think l2u needed to remove some of the vector, not all of it.
    Exactly..

    I want to copy and remove some objects (for instance last 10 in the vector), not all of them.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would suggest taking a look at list's insert method (3rd overload in that reference) and vector's erase method (again the one that takes a range).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To copy x elements, creating a new list:
    Code:
    std::vector<int>::iterator from = vec.end() - x;
    std::list<int> result(from, vec.end());
    from.erase(from, vec.end());
    If the list exists, use insert() instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    To copy x elements, creating a new list:
    Code:
    std::vector<int>::iterator from = vec.end() - x;
    std::list<int> result(from, vec.end());
    from.erase(from, vec.end());
    If the list exists, use insert() instead.
    I always forget about the range setting constructor.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    To copy x elements, creating a new list:
    Code:
    std::vector<int>::iterator from = vec.end() - x;
    std::list<int> result(from, vec.end());
    from.erase(from, vec.end());
    If the list exists, use insert() instead.
    What type should x be?
    Is it int x safe?

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Technically, it should be std::iterator_traits<std::vector<int>::iterator>:: difference_type. In all likelihood, that's a typedef for std:trdiff_t, i.e. a signed integer of platform word size. 32 bits on 32-bit platforms, 64 on 64-bitters. Practically, though, unless you actually have a vector larger than 2 billion elements, int is fine.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM