C++之std::list

概念说明:

List是由双链表实现的一个容器,每个节点存储一个元素,支持前后两种移动方向。List的内存随着添加的节点增加而增加。数据在内存上存储是不连续的。

1、构造与赋值。

list<int> nlist;//一个空的list

list<int> nlist1(5, 3);//list大小为5,初始化的值为3

list<int> nlist2(nlist1);//拷贝构造函数

list<int> nlis3(9);//9个元素,默认为0

list<int> nlis4(nlist2.begin(), nlist2.end());//区间拷贝赋值

list<int> nlis5 = nlis4;//重载等号赋值

auto itr = nlis5.begin();

*itr = 10;//单个元素修改赋值

2、访问方式。

通过迭代器访问:

for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)

cout << *it << " ";

3、添加

nlist.push_back(20);//尾部添加

nlist.push_front(21);//头部添加

nlist.insert(nlist.end(),30);//在指定位置插入一个数据

nlist.insert(nlist.end(), nlist.begin(), nlist.end());//在指定数据插入区间数据,这相当复制了一份

4、删除

nlist.remove(21);//移除数据为21的节点

nlist.erase(nlist.begin());//移除itr=begin的节点

auto firt = nlist.begin();

firt++;

nlist.erase(firt, nlist.end());//删除区间数据

5、迭代器

begin()// 返回指向容器中第一个元素的双向迭代器。

end() // 返回指向容器中最后一个元素所在位置的下一个位置的双向迭代器。

rbegin()// 返回指向最后一个元素的反向双向迭代器。

rend()// 返回指向第一个元素所在位置前一个位置的反向双向迭代器。

cbegin() // 和 begin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

cend()//和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改素。

crbegin() // 和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

crend()// 和 rend() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

6、其他函数

  1. resize(num,elem);//重置大小,若num小于目前大小,则删除后面数据,若大于目前大小,则扩大并且初始值elem.
  2. nlist.swap(nlis5);//两个容器交换
  3. nlist.reverse();//翻转数据
  4. nlist.sort();//排序,默认从小到大
  5. nlist.merge(nlis5);//合并,两个容器必须是有序的,不然会崩溃
  6. auto iter = std::find(nlist.begin(), nlist.end(), 80);//查找元素,返回迭代器

7、优缺点

优点:高效的插入和删除元素。采用动态内存,不会造成内存浪费和溢出。插入操作和删除操作都不会造成原有list 迭代器的失效,这在vector是不成立的

缺点:遍历速度没有数组容器大快,并且占用空间较大(因为链表的指针)。不支持下标访问。

8、例子程序

#include <iostream>

#include<list>

#include<algorithm>

using namespace std;

void printList(const list<int>& L)

{

for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)

{

cout << *it << " ";

}

cout << endl;

}

int main()

{

list<int> nlist;//一个空的list

list<int> nlist1(5, 3);//list大小为5,初始化的值为3

list<int> nlist2(nlist1);//拷贝构造函数

list<int> nlis3(9);//9个元素,默认为0

list<int> nlis4(nlist2.begin(), nlist2.end());//区间拷贝赋值

list<int> nlis5 = nlis4;//重载等号赋值

auto itr = nlis5.begin();

*itr = 10;//单个元素修改赋值

nlist.push_back(20);//尾部添加

nlist.push_front(21);//头部添加

nlist.insert(nlist.end(),30);//在指定位置插入一个数据

nlist.insert(nlist.end(), nlist.begin(), nlist.end());//在指定数据插入区间数据,这里相当复制了一份

cout << "添加操作" << endl;;

printList(nlist);

nlist.remove(21);//移除数据为21的节点

nlist.erase(nlist.begin());//移除itr=begin的节点

auto firt = nlist.begin();

firt++;

nlist.erase(firt, nlist.end());//删除区间数据

cout << "删除操作" << endl;;

printList(nlist);

nlist.clear();

for (int i = 5; i < 16; i++)

nlist.push_back(i);

nlist.resize(10, 33);//重置大小

nlist.reverse();//翻转数据

cout << "翻转" << endl;;

printList(nlist);

nlist.swap(nlis5);//两个容器交换

nlist.sort();//排序,默认从小到大

cout << "排序" << endl;;

printList(nlist);

nlis5.sort();

nlist.merge(nlis5);//合并,两个容器必须是有序的,不然会崩溃

auto iter = std::find(nlist.begin(), nlist.end(), 80);//查找元素,返回迭代器

printList(nlist);

getchar();

}

相关推荐
带土114 小时前
9. C++ 套接字(Socket)
开发语言·c++
fqbqrr20 小时前
2601C++,cmake与导入
c++
fqbqrr21 小时前
2601C++,编写自己模块
c++
王老师青少年编程1 天前
2025年12月GESP真题及题解(C++七级): 城市规划
c++·gesp·csp·信奥赛·七级·csp-s·提高组
寻星探路1 天前
【算法专题】滑动窗口:从“无重复字符”到“字母异位词”的深度剖析
java·开发语言·c++·人工智能·python·算法·ai
我叫袁小陌1 天前
C++多线程全面详解
开发语言·c++
m0_748250031 天前
C++ 官方文档与标准
开发语言·c++
matlabgoodboy1 天前
程序代做python代编程matlab定制代码编写C++代写plc设计java帮做
c++·python·matlab
DYS_房东的猫1 天前
《 C++ 零基础入门教程》第6章:模板与 STL 算法 —— 写一次,用万次
开发语言·c++·算法
点云SLAM1 天前
C++ 静态初始化顺序问题(SIOF)和SLAM / ROS 工程实战问题
开发语言·c++·slam·静态初始化顺序问题·工程实战技术·c++static 关键字