C++list

目录

1、list的介绍

2、list常用接口使用

list的定义

list的迭代器使用

list容量操作

list元素访问

list增删改操作

3、list中迭代器失效问题


1、list的介绍

list的官方文档介绍

1、list容器支持双向迭代。

2、list容器相对于其他容器,能更好的进行插入、删除。

3、list存储数据再内存中不是连续的,其通过指针链接各个节点。

4、list不支持随机访问,如果要访问某个数据,需要从头遍历找到该节点,访问效率很低。

2、list常用接口使用

需要包含头文件#include<list>

list的定义

构造函数constructor

(1)构造一个空list,例如:list();

(2)用n个val构造list

list<int> l1(2, 6);//用2个6构造一个list

(3)用一个现有的list拷贝构造一个新的list

list<int> l1(2, 6);
list<int> l2(l1);

(4)利用迭代器区间构造一个list

list<int> l1(6, 6);
vector<int> v1(5, 3);

//利用l1的数据来构造l2
list<int> l2(l1.begin(), l1.end());
//利用vector的数据构造l3
list<int> l3(v1.begin(), v1.end());

list的迭代器使用

begin  end :begin返回list第一个节点的迭代器,end返回list最后一个节点的下一个位置的迭代器

list<int> l1{ 1,2,3,4,5 };
list<int>::iterator it = l1.begin();
while (it != l1.end())
{
	cout << *it << " ";
    //1  2  3  4  5
	++it;
}
cout << endl;

rbegin  rend:rbegin返回list最后一个节点的迭代器,rend返回第一个节点前一个位置的迭代器。

list<int> l1{ 1,2,3,4,5 };
list<int>::reverse_iterator it = l1.rbegin();
while (it != l1.rend())
{
	cout << *it << " ";
	//5  4  3  2  1
	++it;
}
cout << endl;

正向迭代器++是向后走,反向迭代器++是向前走。

list容量操作

empty :判断list是否为空,如果是空的返回true,否则false

list<int> l1{ 1,2,3,4,5 };
if (l1.empty())
	cout << "空表" << endl;
else
	cout << "非空" << endl;

size:返回list中有效节点的个数

list<int> l1{ 1,2,3,4,5 };
cout << l1.size() << endl;//5

list元素访问

front :返回list的第一个节点中值的引用

list<int> l1{ 1,2,3,4,5 };
cout << l1.front() << endl;//1

back :返回list最后一个节点中值的引用

list<int> l1{ 1,2,3,4,5 };
cout << l1.back() << endl;//5

list增删改操作

push_front :在list第一个有效节点前插入一个节点

list<int> l1{ 1,2,3,4,5 };
l1.push_front(0);//在1前面插入0

pop_front :删除list中第一个有效节点

list<int> l1{ 1,2,3,4,5 };
l1.pop_front();//删除1

push_back :在list最后一个节点后插入一个节点

list<int> l1{ 1,2,3,4,5 };
l1.push_back(6);//在5后面插入6

pop_back:删除list最后一个有效节点

list<int> l1{ 1,2,3,4,5 };
l1.pop_back();//删除5

insert:在pos位置前插入一个节点

list<int> l1{ 1,2,3,4,5 };
auto pos = find(l1.begin(), l1.end(), 3);
l1.insert(pos, 6);

erase :删除pos位置的节点

list<int> l1{ 1,2,3,4,5 };
auto pos = find(l1.begin(), l1.end(), 3);
l1.erase(pos);

swap :交换两个list中的数据(两个list数据类型必须相同)

list<int> l1{ 1,2,3,4,5 };
list<int> l2{ 6,7,8,9,10 };
list<char> l3{ 'a','b','c' };
//交换l1和l2中的数据
l1.swap(l2);
//由于l1和l3类型不同,所以交换失败
//l1.swap(l3);//err

clear :清空list中所有数据

list<int> l1{ 1,2,3,4,5 };
l1.clear();

3、list中迭代器失效问题

之前提到过vector中增容和删除可能会导致迭代器失效;由于list的节点不是连续的,且不存在增容问题,但是删除必定会导致迭代器失效。

list<int> l1{ 1,2,3,4,5 };
auto pos = find(l1.begin(), l1.end(), 3);

//错误
//l1.erase(pos);
//cout << *pos << endl;//err

//正确,如果删除的是最后一个有效节点,就不要去接收了
pos = l1.erase(pos);
cout << *pos << endl;

此时pos指向的数据已经被删除,再去访问必定会出错,所以解决方法和vector一样,用pos重新接收一下erase的返回值(erase返回删除节点的下一个节点的迭代器),如果删除的是最后一个节点,返回的就是list.end()。

C++ 中,list 是双向链表容器,可以用来存储和操作相同类型的数据。list 支持在任意位置插入/删除元素,但是不支持随机访问元素。 以下是 list 的一些常用操作: 1. push_front():在链表头部插入元素。 2. push_back():在链表尾部插入元素。 3. pop_front():删除链表头部元素。 4. pop_back():删除链表尾部元素。 5. insert():在指定位置插入元素。 6. erase():删除指定位置的元素。 7. clear():清空整个链表。 8. size():返回链表中元素的个数。 9. empty():判断链表是否为空。 以下是 list 的一个简单示例: ```c++ #include <iostream> #include <list> using namespace std; int main() { list<int> mylist; // 在链表尾部插入元素 mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); // 在链表头部插入元素 mylist.push_front(0); // 遍历链表并输出元素 for (auto it = mylist.begin(); it != mylist.end(); ++it) cout << *it << " "; cout << endl; // 删除链表头部元素 mylist.pop_front(); // 在指定位置插入元素 auto it = mylist.begin(); ++it; mylist.insert(it, 10); // 删除指定位置的元素 it = mylist.begin(); ++it; mylist.erase(it); // 遍历链表并输出元素 for (auto x : mylist) cout << x << " "; cout << endl; // 清空整个链表 mylist.clear(); // 判断链表是否为空 if (mylist.empty()) cout << "The list is empty." << endl; return 0; } ``` 输出结果: ``` 0 1 2 3 0 2 3 10 The list is empty. ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值