zoukankan      html  css  js  c++  java
  • vector容器

     1 #include<iostream>
     2 #include<Windows.h>
     3 #include<vector>
     4 #include<deque>
     5 using namespace std;
     6 void prinfVector(vector<int> &v)
     7 {//vector迭代器支持随机访问
     8     for (vector<int>::iterator it=v.begin();it!=v.end();it++)
     9     {
    10         //可以修改容器内容
    11         if (*it==3)
    12         {
    13             *it = 33333;
    14         }
    15         cout << *it << "  ";
    16     }
    17     cout << endl;
    18 }
    19 
    20 //扩展,只读访问Vector
    21 void prinfVectorconst(const vector<int> &v)
    22 {//vector迭代器支持随机访问
    23     for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
    24     {
    25         //*it = 100; 无法修改容器内容
    26         cout << *it << "  ";
    27     }
    28     cout << endl;
    29 }
    30 
    31 void test1()
    32 {//初始化构造函数
    33     vector<int> v1;
    34     for (size_t i = 0; i < 10; i++)
    35     {
    36         v1.push_back(i);
    37     }
    38     vector<int> v2(v1);
    39     vector<int> v3(v1.begin(),v1.end());
    40     vector<int> v4(13,5);
    41     cout << "初始化构造函数: " << endl;
    42     prinfVector(v1);
    43     prinfVector(v2);
    44     prinfVector(v3);
    45     prinfVector(v4);
    46 
    47     //赋值
    48     cout << "赋值: " << endl;
    49     vector<int> v5;
    50     v5 = v1;
    51     prinfVector(v5);
    52 
    53     vector<int> v6;
    54     v6.assign(v5.begin(),v5.end());
    55     prinfVector(v6);
    56 
    57     vector<int> v7;
    58     v7.assign(5,19);
    59     prinfVector(v7);
    60 
    61     //容量和大小
    62     cout << "容量和大小: " << endl;
    63     vector<int> v8(v1);
    64     cout << "v8容量" << v8.size() << "v8大小: " << v8.size() << endl;
    65     cout << "v8容量"<<v8.capacity()<<"v8大小: "<<v8.size() << endl;
    66     //删除
    67     if (!v8.empty())
    68     {
    69         v8.pop_back();
    70     }
    71     prinfVector(v8);
    72 
    73     //重置容器大小
    74     v8.resize(20);
    75     prinfVector(v8);
    76     v8.resize(30,9);
    77     prinfVector(v8);
    78     v8.resize(8, 4);
    79     prinfVector(v8);
    80     //插入数据
    81     v8.insert(v8.begin(),2);
    82     prinfVector(v8);
    83     v8.insert(v8.begin(), 5,7);
    84     prinfVector(v8);
    85     //删除容器元素
    86     v8.erase(v8.begin());
    87     prinfVector(v8);
    88     v8.erase(v8.begin(),v8.end());
    89     v8.insert(v8.begin(), 2);
    90     prinfVector(v8);
    91 }
    92 
    93 int main()
    94 {
    95     test1();
    96 
    97     system("pause");
    98     return 0;
    99 }
  • 相关阅读:
    confluence --常用插件整合
    fuse--s3挂载到centos7.5服务器
    gvm--go版本管理工具
    等保1.0与等保2.0的区别
    postfix -- 发件调试
    postfix邮件服务器
    confluence -- 命令行备份还原
    浏览器使用小tip
    windows如何正确下载补丁包
    xwiki使用中的问题
  • 原文地址:https://www.cnblogs.com/rtblogs/p/12095853.html
Copyright © 2011-2022 走看看