Mega Code Archive

 
Categories / C++ / Vector
 

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