zoukankan      html  css  js  c++  java
  • wenbao与vector

    vector

     1 #include <iostream>
     2 #include <vector>
     3 #include <map>
     4 #include <algorithm>
     5 #include <numeric>
     6 using namespace std;
     7 vector<int> v;
     8 int main(){
     9     map<pair<int,int>,int>x;
    10     x[make_pair(1,2)]=4;
    11     cout<<x[make_pair(1,2)]<<endl;
    12 
    13     vector <int > v(10,4);
    14 
    15     v[0]=2;
    16     v[1]=3;
    17 
    18     for(int i=0;i<10;i++)
    19         v.push_back(i);
    20 
    21     v.push_back(10);
    22     v.push_back(20);
    23 
    24     //在最前面插入新元素30
    25     v.insert(v.begin(),30);
    26     v.insert(v.begin()+2,40);
    27     //在向量末尾追加新元素50
    28     v.insert(v.end(),50);
    29 
    30 
    31     //定义变量迭代器
    32     vector<int >::iterator it;
    33 
    34     //删除
    35     //v.erase(v.begin()+2);
    36     //v.erase(v.begin()+1,v.begin()+5);
    37 
    38 
    39     //反向排列向量的从首到尾间的元素
    40     reverse(v.begin(),v.end());
    41 
    42 
    43 
    44     //排序
    45     sort(v.begin(),v.end());
    46 
    47 
    48     for( it=v.begin();it!=v.end();it++)
    49         cout<<*it<<" ";
    50     cout<<endl;
    51 
    52     for(auto it:v)
    53         cout<<it<<" ";
    54     cout<<endl;
    55 
    56     cout<<accumulate(v.begin()+1,v.end(),0)<<endl;
    57 
    58     //输出向量的大小,即包含了多少个元素
    59     cout<<v.size()<<endl;
    60 
    61 
    62     if(v.empty())
    63         cout<<"YES"<<endl;
    64     else
    65         cout<<"NO"<<endl;
    66 
    67 
    68     //清空向量
    69     v.clear();
    70     cout<<v.size()<<endl;
    71 
    72     //删除最后一个元素
    73     v.pop_back();
    74     //得到最后一个元素
    75     v.back();
    76     
    77     return 0;
    78 }

    只有不断学习才能进步!

  • 相关阅读:
    写多了博客,就想沽名钓誉
    中医与DBA
    关于OneProxy推广
    使用分布式数据库集群做大数据分析之OneProxy
    不能使用tpcc-mysql测试OneProxy
    下三滥
    建立自己的客户关系网
    编译spock proxy
    胆子还是小了
    主流语言的异常处理对比
  • 原文地址:https://www.cnblogs.com/wenbao/p/7428756.html
Copyright © 2011-2022 走看看