Checking if One Container Is Less Than or Greater Than Another : transform « deque « C++ Tutorial






#include <algorithm>
#include <deque>
#include <vector>
#include <iostream>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const int data1[] = { 200, 250, 250, 100, 500, 500, 400 };
   const int data2[] = { 200, 200, 300, 500, 400, 400 };

   // create and initialize vectors to hold the debts
   vector<int> debts1( data1,data1 + sizeof( data1 ) / sizeof( data1[0] ) );
   vector<int> debts2( data2,data2 + sizeof( data2 ) / sizeof( data2[0] ) );

   // sort into descending order
   sort( debts1.begin(), debts1.end(), greater<int>() );
   sort( debts2.begin(), debts2.end(), greater<int>() );

   // display debts
   print( debts1 );
   print( debts2 );

   if( debts1 > debts2 )
      cout << "\nCollect from Group 1 first";
   else
      cout << "\nCollect from Group 2 first";

   // store one group of debts in a different container
   deque<int> debts2_deque( debts2.begin(), debts2.end() );

   // compare the vector to the deque
   if( lexicographical_compare( debts2_deque.begin(),
      debts2_deque.end(), debts1.begin(), debts1.end() ) )
      cout << "\nCollect from Group 1 in vector first";
   else
      cout << "\nCollect from Group 2 in deque first";
}








22.12.transform
22.12.1.Transform deque with boolean function
22.12.2.Checking if One Container Is Less Than or Greater Than Another