zoukankan      html  css  js  c++  java
  • STL vector,deque,list

    一.vector
    可变长的动态数组
    必须包含头文件 #include <vector>
    支持随机访问迭代器
    • 根据下标随机访问某个元素时间为常数
    • 在尾部添加速度很快
    • 在中间插入慢
    所有STL算法 都能对vector操作

    构造函数初始化:
    vector();无参构造函数, 将容器初始化成空的
    vector(int n);将容器初始化成有n个元素
    vector(int n, const T & val);假定元素类型是T, 将容器初始化成有n个元素, 每个元素的值都是val
    vector(iterator first, iterator last); 将容器初始化为与别的容器上区间[first, last)一致的内容

    成员函数:
    void pop_back(); 删除容器末尾的元素
    void push_back(const T & val); 将val添加到容器末尾
    int size(); 返回容器中元素的个数
    T & font(); 返回容器中第一个元素的引用
    T & back(); 返回容器中最后一个元素的引用

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int main() {
     5     int i;
     6     int a[5] = { 1,2,3,4,5 };
     7     vector<int> v(5);
     8     cout << v.end() - v.begin() << endl;
     9     for (i = 0; i < v.size(); i++) v[i] = i;
    10     v.at(4) = 100;
    11     for (i = 0; i < v.size(); i++)
    12         cout << v[i] << ",";
    13     cout << endl;
    14     vector<int> v2(a, a + 5); //构造函数
    15     v2.insert(v2.begin() + 2, 13); //在begin()+2位置插入13
    16     for (i = 0; i < v2.size(); i++)
    17         cout << v2.at(i) << ",";
    18 }
    19 //输出:
    20 //5
    21 //0, 1, 2, 3, 100,
    22 //1, 2, 13, 3, 4, 5,

    二维动态数组:
    vector< vector<int> > v(3);
    //v有3个元素,
    //每个元素都是vector<int> 容器


     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int main() {
     5     vector<vector<int >> v(3);
     6     for (int i = 0; i<v.size(); ++i)
     7         for (int j = 0; j<4; ++j)
     8             v[i].push_back(j);
     9     for (int i = 0; i<v.size(); ++i) {
    10         for (int j = 0; j<v[i].size(); ++j)
    11             cout << v[i][j] << " ";
    12         cout << endl;
    13     }
    14     return 0;
    15 } 
    16 //输出:
    17 //0 1 2 3
    18 //0 1 2 3
    19 //0 1 2 3

    二.list

    双向链表 #include <list>
    在任何位置插入/删除都是常数时间
    不支持根据下标随机存取元素
    具有所有顺序容器都有的成员函数

    还支持8个成员函数:
    push_front 在链表最前面插入
    pop_front 删除链表最前面的元素
    sort 排序 (list 不支持 STL 的算法 sort)
    remove 删除和指定值相等的所有元素
    unique 删除所有和前一个元素相同的元素
    merge 合并两个链表, 并清空被合并的链表
    reverse 颠倒链表
    splice 在指定位置前面插入另一链表中的一个或多个元素,并在另一链表中删除被插入的元素


    list容器之sort函数
    list容器的迭代器不支持完全随机访问,不能用标准库中sort函数对它进行排序
    可以用list自己的sort成员函数
    list<T> classname
    classname.sort(compare); //compare函数可以自己定义
    classname.sort(); //无参数版本, 按<排序
    list容器只能使用双向迭代器
    不支持大于/小于比较运算符, []运算符和随机移动(即类似 “list的迭代器+2” 的操作)

      1 #include <list>
      2 #include <iostream>
      3 #include <algorithm>
      4 using namespace std;
      5 class A { //定义类A, 并以友元重载<, ==和<<
      6 private:
      7     int n;
      8 public:
      9     A(int n_) { n = n_; }
     10     friend bool operator<(const A & a1, const A & a2);
     11     friend bool operator==(const A & a1, const A & a2);
     12     friend ostream & operator <<(ostream & o, const A & a);
     13 };
     14 
     15 bool operator<(const A & a1, const A & a2) {
     16     return a1.n < a2.n;
     17 }
     18 bool operator==(const A & a1, const A & a2) {
     19     return a1.n == a2.n;
     20 }
     21 ostream & operator <<(ostream & o, const A & a) {
     22     o << a.n;
     23     return o;
     24 }
     25 
     26 //定义函数模板PrintList, 打印列表中的对象
     27 template <class T>
     28 void PrintList(const list<T> & lst) {
     29     int tmp = lst.size();
     30     if (tmp > 0) {
     31         typename list<T>::const_iterator i;
     32         i = lst.begin();
     33         for (i = lst.begin(); i != lst.end(); i++)
     34             cout << *i << ",";
     35     }
     36 }
     37 
     38 //与其他顺序容器不同, list容器只能使用双向迭代器,
     39 //因此不支持大于/小于比较运算符, []运算符和随机移动
     40 // typename用来说明 list<T>::const_iterator是个类型
     41 //在VS中不写也可以
     42 
     43 int main() {
     44     list<A> lst1, lst2;
     45     lst1.push_back(1); lst1.push_back(3);
     46     lst1.push_back(2); lst1.push_back(4); lst1.push_back(2);
     47     lst2.push_back(10); lst2.push_front(20);
     48     lst2.push_back(30); lst2.push_back(30);
     49     lst2.push_back(30); lst2.push_front(40); lst2.push_back(40);
     50     cout << "1) "; PrintList(lst1); cout << endl;
     51     cout << "2) "; PrintList(lst2); cout << endl;
     52     lst2.sort(); //list容器的sort函数
     53     cout << "3) "; PrintList(lst2); cout << endl;
     54     //1) 1, 3, 2, 4, 2,
     55     //2) 40, 20, 10, 30, 30, 30, 40,
     56     //3) 10, 20, 30, 30, 30, 40, 40,
     57 
     58     lst2.pop_front();
     59     cout << "4) "; PrintList(lst2); cout << endl;
     60     lst1.remove(2); //删除所有和A(2)相等的元素
     61     cout << "5) "; PrintList(lst1); cout << endl;
     62     lst2.unique(); //删除所有和前一个元素相等的元素
     63     cout << "6) "; PrintList(lst2); cout << endl;
     64     lst1.merge(lst2); //合并 lst2到lst1并清空lst2
     65     cout << "7) "; PrintList(lst1); cout << endl;
     66     cout << "8) "; PrintList(lst2); cout << endl;
     67     lst1.reverse();
     68     cout << "9) "; PrintList(lst1); cout << endl;
     69     /*4) 20, 30, 30, 30, 40, 40,
     70     5) 1, 3, 4,
     71     6) 20, 30, 40,
     72     7) 1, 3, 4, 20, 30, 40,
     73     8)
     74     9) 40, 30, 20, 4, 3, 1,*/
     75 
     76     lst2.push_back(100); lst2.push_back(200);
     77     lst2.push_back(300); lst2.push_back(400);
     78     list<A>::iterator p1, p2, p3;
     79     p1 = find(lst1.begin(), lst1.end(), 3);
     80     p2 = find(lst2.begin(), lst2.end(), 200);
     81     p3 = find(lst2.begin(), lst2.end(), 400);
     82     lst1.splice(p1, lst2, p2, p3); //将[p2,p3)插入p1之前,
     83                                    //并从lst2中删除[p2, p3)
     84     cout << "11) "; PrintList(lst1); cout << endl;
     85     cout << "12) "; PrintList(lst2); cout << endl;
     86 }
     87 //11) 40, 30, 20, 4, 200, 300, 3, 1,
     88 //12) 100, 400,
     89 //输出:
     90 //1) 1, 3, 2, 4, 2,
     91 //2) 40, 20, 10, 30, 30, 30, 40,
     92 //3) 10, 20, 30, 30, 30, 40, 40,
     93 //4) 20, 30, 30, 30, 40, 40,
     94 //5) 1, 3, 4,
     95 //6) 20, 30, 40,
     96 //7) 1, 3, 4, 20, 30, 40,
     97 //8)
     98 //9) 40, 30, 20, 4, 3, 1,
     99 //11) 40, 30, 20, 4, 200, 300, 3, 1,
    100 //12) 100, 400,


    三.deque 容器
    双向队列,必须包含头文件 #include <deque>
    所有适用于vector的操作 都适用于deque
    deque还有 push_front (将元素插入到容器的头部)和 pop_front (删除头部的元素) 操作

  • 相关阅读:
    依赖注入
    ToDictionary() and ToList()
    Middleware详解
    仓储层的搭建
    Controller和View的交互
    Configuration配置信息管理
    开发工具
    60分钟Python快速学习(转)
    oracle PL/SQL(procedure language/SQL)程序设计之函数+过程+包(转)
    ssh无密码登陆(转)
  • 原文地址:https://www.cnblogs.com/wanderingzj/p/5343129.html
Copyright © 2011-2022 走看看