sort a vector with duplicates : vector sort « Vector « C++






sort a vector with duplicates

  
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

int main ()
{
    using namespace std;
    typedef vector <string> VECTOR_STRINGS;

    VECTOR_STRINGS v;

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

    v.push_back ("D");

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

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

    return 0;
}
  
    
  








Related examples in the same category

1.Loop through a vector in a reversed order
2.Create const_reverse_iterator out of vector of pairs
3.Create const_iterator out of vector of pairs
4.Sort elements in a vector with sort(scores.begin(), scores.end());