• C++STL模板库序列容器之List容器


    一丶List容器的存储结构

    list容器底层是链表结构来维护的.跟vector不一样. vector是数组维护的.拥有连续内存.所以可以使用[] 运算符操作.list底层是链表维护.内存不连续.所以不能使用[]运算符.
    且对比vector添加了新的方法.因为底层是链表.所以可以对头尾进行删除或者添加元素.

    二丶丶STL中list容器的使用.

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    using namespace std;
    
    
    int main(char *argc, char *argv[]) {
     
      list<int> lst;
      lst.push_back(1);
      lst.push_back(2);
      lst.push_back(4);
      lst.push_back(3); //尾部添加元素
      lst.push_front(5);//头部添加元素
    
      //迭代list容器
      list<int>::iterator it = lst.begin();
    
      lst.sort(less<int>()); //从小到大排序
      lst.sort(greater<int>());//从大到小删除
      
      for (it; it != lst.end(); it++)
      {
        cout << (*it) << endl;  //lst不是连续内存,不支持 [] 操作符.
       // cout << lst.back() << " " << lst.front() << endl;
       
      }
      lst.erase(it);  //删除指定元素.
      lst.pop_back(); //从尾部删除元素
      lst.pop_front();//从头部删除元素
      it = find(lst.begin(), lst.end(), 10);//查找.返回找到的it位置.
      lst.insert(it, 11); //从指定it位置插入.
      cout << "--------------------------" << endl;
      getchar();
      system("pause");
    
    }
    

    总结: 比vector容器多了一个头操作.不支持[]操作符. 有自己的内部排序.

  • 相关阅读:
    shell 案例
    linux 软链接和硬链接区别
    mac安装使用nginx
    Leetcode SQL_#176_第二高的薪水
    南邮ctf web题记录(上)
    CTFHub Web技能树
    XCTF web 新手练习区
    诊断工具--arthas使用教程
    prometheus--监控工具
    无状态状态机--cola stateMachine
  • 原文地址:https://www.cnblogs.com/iBinary/p/9892424.html
Copyright © 2020-2023  润新知