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();

}

相关推荐
乐观勇敢坚强的老彭36 分钟前
C++信息学奥赛lesson1
java·开发语言·c++
Irissgwe1 小时前
C++ STL关联式容器详解:set、multiset、map、multimap
开发语言·c++·stl·set·map·multiset·关联式容器
Irissgwe1 小时前
string类的模拟实现
c++·string
郝亚军1 小时前
Visual Studio 2022安装for C++桌面开发
c++·ide·visual studio
智者知已应修善业2 小时前
【51单片机初始化D5-D8亮,每按键按下D1到D4全亮,再按下恢复,如此循环】2024-3-26
c++·经验分享·笔记·算法·51单片机
为何创造硅基生物2 小时前
C++ 独占指针被销毁后,堆也会自己销毁
c++
C+-C资深大佬2 小时前
C++ 中的 constexpr与 const区
java·开发语言·c++
Tairitsu_H2 小时前
[LC优选算法#3] 滑动窗口 | 将x减到0的最⼩操作数 | ⽔果成篮 | 字⺟异位词
c++·算法·leetcode·滑动窗口
c++之路2 小时前
CMake 系列教程(一):CMake 基础知识
c语言·开发语言·c++
Irissgwe2 小时前
C++ STL bitset 和位图详解
开发语言·c++·stl·位图·bitset