Thread: Insert new element into a vector

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    6

    Insert new element into a vector

    I am trying to insert a new element into a vector in C++ under Ubuntu Linux.

    The following is what I did, where v contain some element already. InsertElementNo is the element number the new content "entry" should be inserted.

    For example:

    v contains "Test,Is,Good"
    If entry = "Lord" and InsertElementNo = 2,
    After v.insert v should look like "Test,Lord,Is,Good"


    Code:
        vector <string> v;
        vector <string>::iterator pos = v.begin();
        .......
        v.insert(pos+(InsertElementNo-1), entry);
    However the above code gives me "segmentation error".

    Couldn't figure out why.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the problem lies in the code that you did not show, e.g., we do not know for sure what is v, pos, InsertElementNo and entry at this point:
    Code:
    v.insert(pos+(InsertElementNo-1), entry);
    In fact, if you tried to populate the vector in the "......." part, pos would likely be invalidated, thus the insertion would be based on an invalid iterator, which could explain the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Quote Originally Posted by appointment View Post
    I am trying to insert a new element into a vector in C++ under Ubuntu Linux.

    The following is what I did, where v contain some element already. InsertElementNo is the element number the new content "entry" should be inserted.

    For example:

    v contains "Test,Is,Good"
    If entry = "Lord" and InsertElementNo = 2,
    After v.insert v should look like "Test,Lord,Is,Good"


    Code:
        vector <string> v;
        vector <string>::iterator pos = v.begin();
        .......
        v.insert(pos+(InsertElementNo-1), entry);
    However the above code gives me "segmentation error".

    Couldn't figure out why.
    How about this?

    Code:
    void do_insertion( vector<string>& v, const string& s, const unsigned int pos )
    {
        vector<string> buff;
        buffer.reserve( v.size()+1 );
        copy( v.begin(), v.begin()+pos, back_inserter(buff) );
        buff.push_back( s );
        copy( v.begin()+pos, v.end(), back_inserter(buff) );
        v.swap( buff);    
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What's this reinventing the wheel? std::vector has a perfectly good insert method. (From performance view, this means that each insertion needs to allocate all that extra memory - which std::vector can avoid, and it means that the entire vector must be copied - imagine you want to insert something to the back part of a large vector.)

    The only thing looking bad in OP's code is how he stores an iterator to vector. Iterators are generally meant for instant use, rather than keeping around for longer, particularly std::vector::iterators as std::vector is rather prone to invalidate them.

    You can't go wrong with
    Code:
    v.insert(v.begin() + pos - 1, entry);
    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).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anon
    You can't go wrong with
    Actually, it should be:
    Code:
    v.insert(v.begin() + InsertElementNo - 1, entry);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    Quote Originally Posted by anon View Post
    What's this reinventing the wheel? std::vector has a perfectly good insert method. (From performance view, this means that each insertion needs to allocate all that extra memory - which std::vector can avoid, and it means that the entire vector must be copied - imagine you want to insert something to the back part of a large vector.)

    The only thing looking bad in OP's code is how he stores an iterator to vector. Iterators are generally meant for instant use, rather than keeping around for longer, particularly std::vector::iterators as std::vector is rather prone to invalidate them.

    You can't go wrong with
    Code:
    v.insert(v.begin() + pos - 1, entry);
    thanks that worked.

    still dont know what's wrong with original way of doing it.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    What probably was wrong is that somewhere in the code that you have simply as ..... , you are changing the size of vector v. If you change a vector, any previously created iterators will become invalid.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    Quote Originally Posted by rossipoo View Post
    What probably was wrong is that somewhere in the code that you have simply as ..... , you are changing the size of vector v. If you change a vector, any previously created iterators will become invalid.
    ahhh i see thanks.

    i was inserting new element into the vector in ..... section by using v.push_back() inside a while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. The proper way to erase a vector element
    By misplaced in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2005, 11:00 AM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM