工作生活

vector(STL)

2019-07-15  本文已影响0人  狼无雨雪
#include<iostream> 
#include<string>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;

void PrintIt(string & S_data){
    cout<<S_data<<endl;
}


void pause(){
    char c;
    cout<<"\n\nPress reture to continue:";
    cin.get(c);
    cout<"\n\n";
}
int main(){
    
//vector的基本使用 
 
//  vector<int> v(5,8);
//  cout<< "v.size() = "<<v.size()<<endl;
//  v.push_back(10);
//  v.push_back(120);
//  cout<<"v.size()"<<v.size()<<endl;
//  
//  for(int i = 0; i<v.size();i++)
//      cout<<v[i]<<endl;
//  v.pop_back();
//  v.pop_back();
//  v.pop_back();
//  v.pop_back();
//  
//  cout<<"v.size()"<<v.size()<<endl;
//  for(int i = 0;i<v.size();i++){
//      cout<<v[i]<<endl;
//  }
//  
//  cout<<"now here is iterator data"<<endl;
//

//迭代器的使用 

//  for(vector<int>::iterator iter = v.begin(); iter<v.end();iter++) {
//      cout<<*iter<<endl;
//  }
    
    
//find_each的使用 
    
//  vector<string> v;
//  v.push_back("English");
//  v.push_back("Math");
//  v.push_back("Chinese");
//  v.push_back("Program");
//  
//  for_each(v.begin(),v.end(),PrintIt);
//  sort(v.begin(),v.end());
//  
//  for_each(v.begin(),v.end(),PrintIt);

//count的使用 

//  vector<int> v;
//  v.push_back(100);
//  
//  v.push_back(1001);
//  
//  v.push_back(999);
//  
//  v.push_back(1340);
//  
//  v.push_back(100);
//  
//  v.push_back(100);
//  
//  cout<<count(v.begin(),v.end(),100)<<endl;
//
//

//find 的使用 

//  vector<int>::iterator iter;
//  iter = find(v.begin(),v.end(),100) ;
//  
//  if(iter == v.end()){
//      cout<<"Not Found !!!"<<endl;
//  }else{
//      cout<<"found"<<endl;
//      cout<<*iter<<endl;
//  }
    
    
    // vector 应用 
    
    vector<int> v(10, 0);
    ostream_iterator<int> out(cout, " ");
    copy(v.begin(),v.end(),out);
    pause();
    
    vector<int>::iterator i = v.begin();
    
    i+=4;
    *i++ = 7;
    *i = 9;
    copy(v.begin(),v.end(),out);
    
    pause();
    
    vector<int>::iterator where = find(v.begin(),v.end(),9);
    
    copy(where,v.end(), out);
    
    pause();
    
    where = v.insert(where, 8);
    
    copy(v.begin(),v.end(),out);
    
    pause();
    
    where += 3;
    
    where = v.insert(where, 4);
    
    copy(v.begin(),v.end(),out);
    
    pause();
    
    where -= 6;
    
    where = v.insert(where, 11);
    
    copy(v.begin(),v.end(), out);
    
    pause();
    
    if(binary_search(v.begin(), v.end(), 8)){
        cout << "yes, 8 occurs in vector v.";
    }else{
        cout<<"No, didn't find 8 in vector v.";
    }
    
    if(binary_search(v.begin(),v.end(), 12)){
        cout<<"yes, 12 occurs in vector v.";
    }else{
        cout<<"No, didn't find 12 in vector v.";
    }
    
    pause();
    
    where = lower_bound(v.begin(),v.end(),8);
    
    copy(where, v.end(),out);
    
    pause();
    
    where = lower_bound(v.begin(),v.end(),0);
    
    copy(where,v.end(),out);
    
    pause();
    vector<int> w(v);
    
    if(v == w){
        cout<<"v and w have the same contents.";
    }else{
        cout<<"v and w have different contents.";
    }
    
    pause();
    
    w[5] = 17;
    
    if(v == w){
        cout<< "v and w have the same contents.";
    }else{
        cout<<"v and w have different contents.";
    }
    
    
    v[5] = 17;
    
    if(v == w){
        cout<<"v and w have the same contents.";
    }else{
        cout<<"v and w have different contents.";
    }
    
    
    
    pause();
    
    copy(v.begin(),v.end(),out);
    
    pause();
    
    
    vector<int>::iterator it;
    for(it = v.begin(); it!=v.end();it++){
        if(*it == 8){
            v.erase(it);
        }
    }
    copy(v.begin(),v.end(),out);
    pause();
    
    
    
    
    
    cout<<"I love My baby !!!"<<endl;
     
     
    
    
    return 0;
}


上一篇下一篇

猜你喜欢

热点阅读