Vector Insert Erase Sort : vector sort « vector « C++ Tutorial






#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print( vector<int> );

int main()
{
    vector<int> vec(5);                              
    print( vec );                                    

    vector<int>::iterator p =  vec.begin();          

    cout << *p << endl;                              

    vec.insert( vec.begin(), 9 );                    
    print( vec );                                    

    cout << *p << endl;                              

    p = vec.begin();                                 
    cout << *p << endl;                              
    vec.erase( vec.begin() );                        
    print( vec );                                    

    vec.insert( vec.begin() + 2, 8 );                
    print( vec );                                    

    vec.erase( vec.begin() + 2 );                    
    print( vec );                                    

    vec.insert( vec.end(), 7 );                      
    print( vec );                                    

    vec.erase( vec.end() - 1 );                      
    print( vec );                                    

    vec.insert( vec.begin() + 3, 6 );                
    print( vec );                                    
    vec.erase( find( vec.begin(), vec.end(), 6 ) );  
    print( vec );                                    

    vec.insert( vec.begin() + 1, 3 );                
    vec.insert( vec.begin() + 5, 3 );                
    print( vec );                                    
    vec.erase( find( vec.begin(), vec.end(), 3 ) );  
    vec.erase( find( vec.begin(), vec.end(), 3 ) );  
    print( vec );                                    

    vec[0] = 23;                                     
    vec[1] = 2;
    vec[2] = 16;
    vec[3] = 45;
    vec[4] = 16;
    print( vec );                                    

    sort( vec.begin(), vec.end() );                  
    print( vec );                                    

    return 0;
}

void print( vector<int> v ) {
     vector<int>::iterator p = v.begin();
     while ( p != v.end() )
         cout << *p++ << " ";
     cout << endl;
}








16.29.vector sort
16.29.1.sort the smallest 4 elemetns in the Numbers and copy the results in Result
16.29.2.sort NumbersVestor, mersge requires the sequences to be sorted
16.29.3.Vector Insert Erase Sort