Thread: STL vectors

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    STL vectors

    I'm a novice when it comes to the STL. What I need to do is remove an element from anywhere in the list but it seems like I can only use pop_back() to remove the end element. The only way I can think of doing it is swapping the end element with the element I want to delete and then call pop_back() but there must be an easier way right?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Sure, use vector::erase. The annoying thing is that you have to use iterators . Here's a reference:http://msdn2.microsoft.com/en-us/library/ceh559x2.aspx

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    if youre using vectors then this is the best solution if the ordering of your elements don't matter.

    [edit]
    hm... if im not mistaken (and i think im not )erase shifts all elements with an index greater than the index of the element to erase down by 1.
    so if you quickly want to remove an element, use the swapping method

    [/edit]
    Last edited by Raven Arkadon; 12-05-2005 at 10:13 PM.
    signature under construction

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is an example:
    Code:
    #include <vector>
    #include <algorithm>  // for find_if
    #include <iterator>
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    	vector<int> vec;
    
    	for (int i = 0; i < 10; i++)
    	{
    		vec.push_back(i);
    	}
    
    	cout << endl;
    	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
    
    	vec.erase(vec.begin() + 5);
    	vec.erase(vec.begin() + 7);
    
    	cout << endl;
    	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
    
    	/* Use the swap method to delete vec[1]. */
    	iter_swap(vec.begin() + 1, vec.end() - 1);
    	vec.erase(vec.end() - 1);
    
    	cout << endl;
    	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
    
    	cin.get();
    }
    Output:
    Code:
    0 1 2 3 4 5 6 7 8 9
    0 1 2 3 4 6 7 9
    0 9 2 3 4 6 7
    Here is the STL documentation.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What I need to do is remove an element from anywhere in the list but it seems like I can only use pop_back() to remove the end element
    A vector is not referred to as a list. The reason for that is that there is another STL container called a <list>, which is a linked list. A list is actually what you should be using instead of a vector. Erasing an element in a vector, means the vector has to take all the elements to the right of the removed element and shuffle them to the left in order to fill the gap. That is inefficient.

    If you erase an element in a linked list, the element is detached from the two elements to the left and the right, and then the left and right elements are reattached to each other. That is more efficient than shuffling over all the elements to the right of the erased element to fill the gap.
    Last edited by 7stud; 12-05-2005 at 10:58 PM.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Thanks for all the info. I wasn't aware of list however I was going to write my own linked list but I didn't because I thought vectors will do the job. I will try the list approach.

    [edit]
    one problem: vector seems to be the only one that uses [] operators to access elements...
    Last edited by sand_man; 12-06-2005 at 01:00 AM.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Deque and map use [] as well. Deque is slight improvement over vector because you can push/pop_front().
    Last edited by Darryl; 12-06-2005 at 01:32 AM.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Ok it's all done. I used vector and it works fine thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sets of Vectors in STL
    By Steff in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2009, 12:03 AM
  2. STL Vectors of Vectors
    By Beamu in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2008, 05:23 AM
  3. allocating structs within STL vectors
    By aoiOnline in forum C++ Programming
    Replies: 20
    Last Post: 12-05-2007, 02:49 PM
  4. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  5. STL Vectors
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2005, 08:35 PM