Appending values to Vector Containers after sorting : vector insert « Vector « C++






Appending values to Vector Containers after sorting

  
#include <algorithm>
#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 char* names1[] = { "A", "B", "C" };
   const char* names2[] = { "D", "E", "F", "G" };

   vector<string> squad1( names1,names1 + sizeof( names1 ) / sizeof( names1[0] ) );
   vector<string> squad2( names2,names2 + sizeof( names2 ) / sizeof( names2[0] ) ); 

   sort( squad1.begin(), squad1.end() );
   
   print( squad1);
   sort( squad2.begin(), squad2.end() );
   print( squad2);

   squad1.insert( squad1.end(), squad2.begin(), squad2.end() );
   print( squad1); 

   sort( squad1.begin(), squad1.end() );
   print( squad1);
}
  
    
  








Related examples in the same category

1.Insert element by index
2.Insert element at specific position
3.Insert 10 duplicate value to vector
4.Insert one vector to another vector
5.Insert elements from array
6.Combine insert and end to add elements to the end of vector
7.Insert characters into vector. An iterator to the inserted object is returned
8.Create and initialize vector with float number array
9.Inserts an element into a vector at given position