zoukankan      html  css  js  c++  java
  • 【STL】- vector的用法

    初始化:

    1. 默认构造:

    vector<int> vint;

    2. 用包含10个元素的数组初始化:

    vector<int> vint(ia, ia+10);

    算法:


    1. vint.push_back(i);

    2. vint.size();

    3. vint[i];

    4. vint.erase(pos1, pos2);

     代码:

     1 #include <vector>
     2 #include <iostream>
     3 #include <iterator>
     4 using namespace std;
     5 
     6 int ia[] = {1,2,3,4,5,6,7,8,9,10, 11,12};
     7 
     8 int main() {
     9     //initialize
    10     vector<int> vint1;
    11     vector<int> vint2(ia, ia+10);
    12 
    13     //push_back()
    14     for(int i = 0; i <=6; ++i)
    15         vint1.push_back(i);
    16 
    17     //no push_front()
    18     //vint1.push_front(222);
    19 
    20     //size()
    21     cout << "The size of vint1 is: " << vint1.size() << endl;
    22 
    23     //[] operator
    24     for(int i = 0; i < vint2.size(); i++)
    25         cout << vint2[i] << endl;
    26 
    27     vint2.erase(vint2.begin()+3, vint2.end());
    28 
    29     copy(vint2.begin(), vint2.end(), ostream_iterator<int>(cout, " "));
    30 
    31     return 0;
    32 }

    输出:

    $ ./a.exe
    The size of vint1 is: 7
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    1 2 3

     

  • 相关阅读:
    centos安装libreoffice
    世界,你好!
    4.闭包函数
    3.回调函数
    1内存地址
    2.函数递归
    1.字典
    nonlocal可以修改外层函数变量
    单例模式(Singleton)示例源码
    大家来说说自己的WEB开发工具好吗?
  • 原文地址:https://www.cnblogs.com/dracohan/p/3895305.html
Copyright © 2011-2022 走看看