C++之vector

  1 #include <iostream>
  2 #include <vector>
  3 
  4 using  namespace std;
  5 
  6 int main(){
  7     //vector容器 是一个动态数组
  8     //<>里面表示的是模板参数 vector为类模板
  9     vector<int> ivec; // 保存int类型数据的向量
 10     vector<int> a; //a是一个空的容器
 11     vector<int> b(10,2); //b这个容器不是空的
 12 
 13     a.push_back(1); //往向量容器中放数据 往后放 第一个下标是0
 14     a.push_back(2);
 15     a.push_back(3);
 16 
 17     b.push_back(10);
 18     b.push_back(11);
 19 
 20     cout<<a.size()<<endl;
 21     cout<<b.size()<<endl;
 22     //循环输出
 23     for(vector<int>::size_type i=0;i!=a.size();i++)
 24     {
 25         cout<<a[i]<<endl;
 26     }
 27 
 28     for(vector<int>::size_type i=0;i!=b.size();i++)
 29     {
 30         cout<<b[i]<<endl;
 31     }
 32 
 33     cout<<"======================================"<<endl;
 34 
 35     vector<int> v1;
 36     v1.push_back(10);
 37     v1.push_back(11);
 38     v1.push_back(12);
 39     vector<int> v2(v1);
 40 
 41     for(vector<int>::size_type i=0;i!=v1.size();i++)
 42     {
 43         cout<<v1[i]<<endl;
 44     }
 45     cout<<"======================================"<<endl;
 46 
 47     //动态输入字符串到vector容器中
 48     cout<<"请输入字符串:"<<endl;
 49     string word;
 50     vector<string> text;
 51     while(cin>>word)
 52     {
 53         text.push_back(word);
 54     }
 55     //输出
 56     cout<<"输入字符串是:"<<endl;
 57      for(vector<string>::size_type i=0;i!=text.size();i++)
 58     {
 59         cout<<text[i]<<endl;
 60     }
 61 
 62     cout<<"======================================"<<endl;
 63 
 64     //
 65     cout<<"请输入数据:"<<endl;
 66     vector<int> x;
 67     int k;
 68     while(cin>>k)
 69     {
 70         x.push_back(k);
 71     }
 72 
 73     //计算相邻的两个数之和 先判断是否为空
 74     if(x.empty())
 75     {
 76         cout<<"向量为空"<<endl;
 77         return -1;
 78     }
 79 
 80     for(vector<int>::size_type i=0;i!=x.size()-1;i+=2)
 81     {
 82         cout<<x[i]+x[i+1]<<endl;
 83     }
 84     if(x.size()%2!=0){
 85         cout<<"最后一个直接输出,没有相加";
 86         cout<<x[x.size()-1]<<endl;
 87     }
 88 
 89     //首尾相加
 90     vector<int>::size_type first,last;
 91     for(first=0,last=x.size()-1;first<last;++first,--last)
 92     {
 93         cout<<x[first]+x[last]<<endl;
 94     }
 95 
 96     //=============================================
 97     string str;
 98     vector<string> str1;
 99     cout<<"enter text"<<endl;
100     while(cin>>str)
101     {
102         str1.push_back(str);
103     }
104     if(str1.empty())
105     {
106         return -1;
107     }
108     for(vector<string>::size_type i=0;i!=str1.size();i++)
109     {
110         //对每一个元素里面的字符串进行循环 转换成大写
111         for(string::size_type j=0;j!=str1[i].size();j++)
112         {
113             if(islower(str1[i][j]))
114             {
115                 str1[i][j]=toupper(str1[i][j]);
116                 cout<<str1[i]<<" ";
117                 if((i+1)%8==0)
118                 {
119                     cout<<endl;
120                 }
121             }
122         }
123     }
124 
125     return 0;
126 }

 

posted @ 2020-03-19 10:41  萌萌~  阅读(110)  评论(0编辑  收藏  举报