sorting a vector after erasing element?

This is a discussion on sorting a vector after erasing element? within the C++ Programming forums, part of the General Programming Boards category; I sometimes need to erase an element from a vector using erase function. This works, but do I need to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    sorting a vector after erasing element?

    I sometimes need to erase an element from a vector using erase function. This works, but do I need to sort the vector later? So there are no gaps in the vector...
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I think std::vector's erase method removes that node, so there will be no gap. Also, removing nodes does not affect how the vector has been sorted (for the most part), adding will though.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    So no sorts. Anyone else?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There won't be any gaps in the vector.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The whole point in using vectors is because they're self sizing and you can erase elements and insert elements. Of course, that would take you about 3 minutes to test, so I can see why you wouldn't want to be bothered.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    for vectors use the erase-remove trick
    Code:
    vector<int> vec;
    // load vector with ints.
    // now lets erase all elements that hold 100
    vec.erase(remove(vec.begin(),vec.end(),100),vec.end());
    // wanna erase by predicate.... like this..
    vec.erase(remove_if(vec.begin(),vec.end(),MyPredicate),vec.end());
    std:: omitted for brevity
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    i am working on a program containing this right now. i think that you have to make a for loop:

    for(int i=0; i<array.length; i++)
    {
    if(array[i]==" ")
    {
    array[i]==array[i+1];
    }
    array[max index value]=" ";
    }
    i think this should work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  2. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21