Thread: using push_back on an iterator

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    using push_back on an iterator

    hi all
    i want to be able scan text moving from word to word with an iterator and which ever that word the iterator is on push it into a vector

    this is what i have but i get an error
    Code:
    string format_paragraph(vector<string> words, int text_width)
    
    vector<string>::iterator pos;
    for(pos=words.end();pos != words.begin(); pos++)
    words.push_back(pos);

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    push_back takes a value_type. Try dereferencing the iterator.
    Code:
    words.push_back(*pos);
    Kampai!

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    nice thanx a lot it works but the only problem is it segfaults.
    any other ideas?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by twigboy
    Code:
    for(pos=words.end();pos != words.begin(); pos++)
    What is wrong with that code?

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    well i should proly decrement pos so it should be pos-- but other than that i dont know

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Good start.

    Look into reverse iterators if you really want to iterate in reverse.

    Also, always remember that you shouldn't use the iterator at foo.end() - it is bad to use it by dereferencing, you might just get a seg fault - the only reason it is there is for comparison to see if you are done.

  7. #7
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Quote Originally Posted by Daved
    What is wrong with that code?
    Ah, tunnel vision sucks.
    Code:
    for (pos = words.end() - 1; pos != words.begin(); --pos)
      words.push_back(*pos);
    words.push_back(*pos);
    However, notice that the first item in the vector is ignored by the loop since it's a stopping condition. That's why I added another push_back after the loop. A better solution is what Daved suggested.
    Code:
    vector<string>::reverse_iterator pos;
    for (pos = words.rbegin(); pos != words.rend(); ++pos)
      words.push_back(*pos);
    Kampai!

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    thanx guys thats awsome

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Isn't the push_back operation going to invalidate the iterator? Once you start pushing the strings back onto the very same vector you are iterating over it is going to invalidate those iterators you are using in the loop. Am I the only one that sees this? You shouldn't iterate over the same container that you are simultaneously modifying. You probably need to use a second vector to push onto.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Am I the only one that sees this?
    I was thinking the same thing, but thought maybe I was missing something. It seems you would need a second vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-19-2008, 11:16 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM