C++/Vector/vector sort

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Create const_iterator out of vector of pairs

  
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,string> Pair;
inline bool less_than_second( const Pair& b1, const Pair& b2 ){
   return b1.second < b2.second;
}
int main( )
{
   const char* names[] = { "A","B", "C", "D","E" };
   const int values[] = { 18, 20, 26, 30, 41 };
   const int num_pairs = sizeof( names ) / sizeof( names[0] );
   vector<Pair> pair( num_pairs );
   transform( values, values+num_pairs, names,pair.begin(), make_pair<int,string> );
   sort( pair.begin(), pair.end() );
   sort( pair.begin(), pair.end(), less_than_second );
   vector<Pair>::const_iterator pair_end = pair.end();
   for( vector<Pair>::const_iterator i = pair.begin();
      i != pair_end; ++i )
   cout << i->second << " - $" << i->first << " values\n";
}


Create const_reverse_iterator out of vector of pairs

  
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,string> Pair;
inline bool less_than_second( const Pair& b1, const Pair& b2 ){
   return b1.second < b2.second;
}
int main( )
{
   const char* names[] = { "A","B", "C", "D","E" };
   const int values[] = { 18, 20, 26, 30, 41 };
   const int num_pairs = sizeof( names ) / sizeof( names[0] );
   vector<Pair> pair( num_pairs );
   transform( values, values+num_pairs, names,pair.begin(), make_pair<int,string> );
   sort( pair.begin(), pair.end() );
   vector<Pair>::const_reverse_iterator pair_rend = pair.rend();
   
   for( vector<Pair>::const_reverse_iterator i= pair.rbegin(); i != pair_rend; ++i )
      cout << i->first << " - " << i->second;
   sort( pair.begin(), pair.end(), less_than_second );
   vector<Pair>::const_iterator pair_end = pair.end();
   for( vector<Pair>::const_iterator i = pair.begin();
      i != pair_end; ++i )
   cout << i->second << " - $" << i->first << " values\n";
}


Loop through a vector in a reversed order

  
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,string> Pair;
inline bool less_than_second( const Pair& b1, const Pair& b2 ){
   return b1.second < b2.second;
}
int main( )
{
   const char* names[] = { "A","B", "C", "D","E" };
   const int values[] = { 18, 20, 26, 30, 41 };
   const int num_pairs = sizeof( names ) / sizeof( names[0] );
   vector<Pair> pair( num_pairs );
   transform( values, values+num_pairs, names,pair.begin(), make_pair<int,string> );
   sort( pair.begin(), pair.end() );
   vector<Pair>::const_reverse_iterator pair_rend = pair.rend();
   
   for( vector<Pair>::const_reverse_iterator i= pair.rbegin(); i != pair_rend; ++i )
      cout << i->first << " - " << i->second;
   sort( pair.begin(), pair.end(), less_than_second );
   vector<Pair>::const_iterator pair_end = pair.end();
   for( vector<Pair>::const_iterator i = pair.begin();
      i != pair_end; ++i )
   cout << i->second << " - $" << i->first << " values\n";
}


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;
}


Sort elements in a vector with sort(scores.begin(), scores.end());

  
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
    vector<int>::const_iterator iter;
    vector<int> scores;
    scores.push_back(1);
    scores.push_back(3);
    scores.push_back(5);
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
    sort(scores.begin(), scores.end());
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
 return 0;
}