zoukankan      html  css  js  c++  java
  • c++常见容器操作

    总结下常见c++容器操作。

    1.Vector、 list和 deque
    Vector是数组,List 是双端链表,deque时双向队列。分别引入vector/list/deque库。

    vector、deque支持随机访问和迭代器偏移,list、deque支持双向访问,list只能自加自减。

    push_back、insert

    push_front 【vector不可用】

    resize 【list不可用】

    pop_back、erase

    pop_front 【vector不可用】

    resize 【list不可用】

    下标、迭代器 【list不可用下标】

    front、back 【vector不可用front】

    演示如下:

    //Vector -- 数组
    //List -- 双端链表 多了些自己的merge、split、find操作
    //deque --双向队列
    void VectorAndListShow()
    {
    cout << "=============vector" << endl;
    vector<int> v;

    //push_back-增加
    v.push_back(12);
    v.push_back(13);
    v.push_back(14);
    v.insert(v.begin(), 15);

    //下标方式遍历
    for (int i=0; i<v.size(); i++)
    {
    cout << v[i] << " " << endl;
    }

    //迭代器方式遍历
    for (vector<int>::iterator iter= v.begin(); iter!=v.end(); iter++)
    {
    cout << *iter << " " << endl;
    }

    //erase-删除,注意更新迭代器
    vector<int>::iterator iter = v.begin();
    iter = v.erase(iter);//删除后迭代器返回的是下一个元素,此时要更新迭代器
    cout << "first value after delete:" << *iter <<endl;

    //pop_back-删除
    v.pop_back();

    //reserve/resize-容量
    v.reserve(50);
    cout << "Capacity" << v.capacity() << endl; //Capacity 总分配空间大小
    cout << "Size" << v.size() << endl; //size 程序已经占用的空间大小
    v.resize(10);
    cout << "Capacity" << v.capacity() << endl;
    cout << "Size" << v.size() << endl;

    cout << "=============list" << endl;
    list<int> l;

    //push_front/push_back-前增
    l.push_front(10);
    l.push_front(12);
    l.push_front(13);
    l.push_front(14);

    for (list<int>::iterator iter=l.begin(); iter != l.end(); iter++)//只能用!=,不能用<
    {
    cout << *iter << "-";
    }
    cout << endl;

    //front/back-访问首尾
    cout << l.front() << endl;
    }
     

    2.stack、 queue、 priority_queue
     

    这三种容器都是前面叙述的容器的包装容器,stack、queue默认使用deque实现,priority_queue默认使用vector实现,分别引入stack和queue库。可以更换默认的包装容器。

    【a】.stack-后进(push)先出(pop) 

    支持push/pop/top/empty

    如下,使用list容器来实现stack

    //stack-后进(push)先出(pop)
    stack<int, list<int> > s;//注意里面的<>和外面的<>分离
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);

    while(!s.empty())
    {
    int si = s.top();
    s.pop();
    cout << si << " " << endl;
    }
     

    【b】.queue-先进(push)先出(pop)

    支持push/pop/front/top/empty

    如下

    //queue-先进(push)先出(pop)
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    q.push(5);

    while(!q.empty())
    {
    int si = q.front();//或back
    q.pop();
    cout << si << " " << endl;
    }

    【c】.priority_queue-支持优先级的队列,可以自定义优先级

    支持push/pop/top/empty

    如下,实现数字越小则优先级越高,越先输出的队列

    priority_queue<int, vector<int>, less<int>> pq;
    pq.push(18);
    pq.push(22);
    pq.push(31);
    pq.push(48);
    pq.push(52);

    while(!pq.empty())
    {
    int si = pq.top();//不能用front和back
    pq.pop();
    cout << si << " " << endl;
    }
     

    3.map、 set、 multimap 和multiset
     

    本质上map和set都是hash实现,可以实现复杂度为O(1)的查找和插入复杂度,分别引入map和set库。

    【a】.map和set基本操作

    支持 insert/erase/count/find

    演示如下:

    map<int, string> m;

    //[]下标操作,没有的会新建
    for (int i=0; i<4; i++)
    {
    m[i] = string("12345");
    }

    //直接插入,make_pair需要引入ulity库
    m.insert(make_pair(11, "11"));
    m.insert(make_pair(12, "12"));
    m.insert(make_pair(13, "13"));
    m.insert(make_pair(14, "14"));

    //遍历输出
    for (map<int,string>::iterator iter=m.begin(); iter!=m.end(); iter++)
    {
    cout << iter->first << "->" << iter->second.c_str() << endl;
    }

    //删除
    int nCnt = m.erase(11);//删除键值为11的,返回值为本次删除的元素个数,一定是1
    cout << nCnt << endl;

    map<int,string>::iterator delEnd = m.begin();
    delEnd++;//delEnd不能加,只能自增
    m.erase(m.begin(), delEnd);//只删除,不返回值

    //查找
    int n = m.count(13);//对应值的个数,0或1
    cout << "The count of 13 is:" << n << endl;
    map<int,string>::const_iterator iter = m.find(13);//返回对应元素的迭代器
    cout << iter->first << "find is" << iter->second.c_str() << endl;

    //set 和map类似,用于记录单一数据,快速查询
    set<int> s;
    s.insert(1);
    s.insert(2);
    s.insert(3);
    s.insert(4);

    cout << s.count(5) << endl;
     

    【b】.multimap和multiset
    对于multimap和multiset,可存在多个相同的key

    相同的key在存储上位置一定是连续的,因此才有如下3种访问指定key元素的方法:

    //指定值的全部元素 find+count
    multimap<int,int>::iterator iterFind = mm.find(3);
    for (int i=0; i<mm.count(3); i++, iterFind++)
    {
    cout << iterFind->first << "==>>" << iterFind->second << endl;
    }

    //指定值的全部元素 lower_bound和upper_bound
    multimap<int,int>::iterator iterBegin = mm.lower_bound(3);
    multimap<int,int>::iterator iterEnd = mm.upper_bound(3);
    while (iterBegin!=iterEnd)
    {
    cout << iterBegin->first << " ==>> " << iterBegin->second << endl;
    iterBegin++;
    }

    //指定值的全部元素 equal_range
    pair<multimap<int,int>::iterator,multimap<int,int>::iterator> iterPos = mm.equal_range(3);
    while (iterPos.first!=iterPos.second)
    {
    cout << iterPos.first->first << " ==>> " << iterPos.first->second << endl;
    iterPos.first++;
    }
    ————————————————
    版权声明:本文为CSDN博主「文大侠」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/wenzhou1219/article/details/51533059

  • 相关阅读:
    八数码问题 Eight Digital Problem
    What is probabilistic programming? | 中文翻译
    Installing Moses on Ubuntu 16.04
    Python3 解析XML 层序遍历二叉树
    ORACLE之PACKAGE-包、存储过程、函数
    Java解析XMl文件之SAX和DOm方法
    关于Could not parse configuration: /hibernate.cfg.xml的问题
    hibernate入门
    linux常用命令
    基本STRUTS标签-学习笔记-Bean标签
  • 原文地址:https://www.cnblogs.com/mjgw/p/12618517.html
Copyright © 2011-2022 走看看