C++/Vector/vector compare — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:27, 25 мая 2010

Checking If Two Containers Are Equal with == operator

  
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main( )
{
   short v[] = { 1, 2, 3, 4, 5, 6 };
   vector<short> v1( v,v + sizeof( v ) / sizeof( v[0] ) );
   vector<short> v2( v1 );
   if( v1 == v2 )
      cout << "The vectors are equal" << endl;
   else
      cout << "The vectors are not equal"  << endl;
   reverse( v2.begin(), v2.end() );
   if( v1 == v2 )
      cout << "The vector and reversed vector are equal" << endl;
   else
      cout << "The vector and reversed vector are not equal" << endl;
}


Compare two containers

   
#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<char> vect);
int main() {
  vector<char> v;
  vector<char> v2(v);
  if(v == v2) 
     cout << "v and v2 are equivalent.\n\n";
  cout << "Insert more characters into v and v2.\n";
  v.insert(v.end(), "D");
  v.insert(v.end(), "E");
  v2.insert(v2.end(), "X");
  show("The contents of v: ", v);
  show("The contents of v2: ", v2);
  cout << "\n";
  if(v < v2) 
     cout << "v is less than v2.\n\n";
  v.insert(v.begin(), "Z");
  show("The contents of v: ", v);
  if(v > v2) 
     cout << "Now, v is greater than v2.\n\n";
  v2.erase(v2.begin());
  show("v2 after removing the first element: ", v2);
  vector<char> v3;
  v3.insert(v3.end(), "X");
  v3.insert(v3.end(), "Y");
  v3.insert(v3.end(), "Z");
  show("The contents of v3: ", v3);
  cout << "Exchange v and v3.\n";
  v.swap(v3);
  show("The contents of v: ", v);
  show("The contents of v3: ", v3);
  v.clear();
  if(v.empty())  
     cout << "v is now empty.";
  return 0;
}
void show(const char *msg, vector<char> vect) {
  vector<char>::iterator itr;
  cout << msg << endl;
  for(itr=vect.begin(); itr != vect.end(); ++itr)
    cout << *itr << endl;
}


Compare two vectors: !=

   
 
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <vector>
using std::vector;
int main()
{
   vector< int > integers1( 7 ); // 7-element vector< int >
   vector< int > integers2( 10 ); // 10-element vector< int >
   size_t i;
   
   for ( i = 0; i < integers1.size(); i++ ){
      cout << integers1[ i ];
   }
   for ( i = 0; i < integers2.size(); i++ ){
      cout << integers2[ i ];
   }
   cout << "\nEvaluating: integers1 != integers2" << endl;
   if ( integers1 != integers2 )
      cout << "integers1 and integers2 are not equal" << endl;
   return 0;
}
        
 /* 
00000000000000000
Evaluating: integers1 != integers2
integers1 and integers2 are not equal
 */


vector assignment

   
 
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <vector>
using std::vector;
int main()
{
   vector< int > integers1( 7 ); // 7-element vector< int >
   vector< int > integers2( 10 ); // 10-element vector< int >
   size_t i;
   
   for ( i = 0; i < integers1.size(); i++ ){
      cout << integers1[ i ];
   }
   for ( i = 0; i < integers2.size(); i++ ){
      cout << integers2[ i ];
   }
   cout << "\nAssigning integers2 to integers1:" << endl;
   integers1 = integers2; // integers1 is larger than integers2
   // use equality (==) operator with vector objects
   if ( integers1 == integers2 )
      cout << "integers1 and integers2 are equal" << endl;

   return 0;
}
/* 
00000000000000000
Assigning integers2 to integers1:
integers1 and integers2 are equal
 */