Erase adjacent duplicates : vector erase « vector « C++ Tutorial






#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef vector <string> VECTOR_STRINGS;
int main (){
    VECTOR_STRINGS v;

    v.push_back ("A");
    v.push_back ("B");
    v.push_back ("C");
    v.push_back ("D");

    // insert a duplicate into the vector
    v.push_back ("D");

    VECTOR_STRINGS::iterator i;

    // Erase adjacent duplicates
    i = unique (v.begin (), v.end ());
    v.erase (i, v.end ());

    for (size_t nItem = 0; nItem < v.size (); ++ nItem){
        cout << "Name [" << nItem << "] = \"";
        cout << v [nItem] << "\"" << endl;
    }

    return 0;
}








16.13.vector erase
16.13.1.Remove(delete) all elements in the vector
16.13.2.Erase first element
16.13.3.erase the numbers 2 through 5 in v1
16.13.4.insert and erase.
16.13.5.Use unique_copy to remove duplicate words
16.13.6.Erase adjacent duplicates
16.13.7.Erase all value in a vector more than three standard deviations greater than the mean
16.13.8.Erase all value in a vector more than three standard deviations less than the mean with erase() remove_if() and bind2nd