实验1.1熟悉string和vector

#include <iostream>
#include <string>
#include <vector> 
int main() {
    using namespace std;
    string s1;
    string s2{"c plus plus"};
    string s3{s2};
    string s4=s2;
    
    s1="oop";
    vector<string>v1;
    v1.push_back(s1);
    v1.push_back(s2+"1");
    v1.push_back(s3+"2");
    v1.push_back(s4+"3");
    
    cout<<"output1: "<<endl;
    for(auto item: v1){
        cout<<item<<endl;
    }
    
    cout<<"output2: ";
    for(auto p=v1.begin();p!=v1.end();++p){
        cout<<*p<<endl;
    }
    
    cout<<"output3: ";
    for(auto i=0;i<v1.size();++i){
        cout<<v1[i]<<endl;
    }
    vector<string>v2{v1.rbegin(),v1.rend()};
    cout<<"v2: "<<endl;
    for(auto item:v2){
        cout<<item<<endl;
    }
}

 

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <time.h>
 7 
 8 template<typename T>
 9 void output(const T& obj){
10     for(auto item: obj)
11     std::cout<<item<<" ";
12     std::cout<<std::endl;
13 } 
14 
15 int main(){
16     using namespace std;
17     vector<int>v1{1,9,8,4};
18     v1.insert(v1.begin(),2022);
19     v1.insert(v1.end(),2023);
20     
21     cout<<"v1: ";
22     output(v1);
23     v1.pop_back();
24     v1.erase(v1.begin());
25     cout<<"v1: ";
26     output(v1);
27     
28     vector<string> v2{"《1984》"," 《动物农场》","《美丽新世界》"};
29     cout<<"v2: ";
30     output(v2);    
31 }

 

posted @ 2022-09-28 16:44  ~GZJ  阅读(10)  评论(0编辑  收藏  举报