list容器类型

list使用一个doubly linked list(双向链表)来管理元素。

使用list时必须包含头文件<list>

#include <list>

其中list型别定义于namespace std中,是个class template:

namespace std {
	template<class T,
			class Allocator = allocator<T> >
	class list;
}

优缺点:

①list不支持随机存取。

②任何位置上(不只是两端)执行元素的安插和移除都非常快,始终都是常数时间内完成,因为无需移动任何其他元素。(实际只是内部指针操作而已)

③安插和删除动作不会造成指向其他元素的各个pointers、references、iterators失效。

④list对于异常处理有这样的方式:要么操作成功,要么什么都不发生。

list<int> c(c1);

list<int> c;
list<int> c(n,elem);
list<int> c(beg,end);
c.size();
c.empty();
c.max_size();
c1 = c2;
c.assign(c,elem);
c.assign(beg,end);
c1.swap(c2);
swap(c1,c2);//全局函数
c.front();//不检查第一个元素是否存在
c.back();//不检查最后一个元素是否存在
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(pos,elem);
c.insert(pos,n,elem);
c.insert(pos,beg,end);
c.push_back(elem);
c.pop_back();//移除最后一个元素,但是不回传
c.push_front(elem);
c.pop_front();//移除头部元素,但是不回传
c.remove(val);//移除所有值为val的元素
c.remove_if(op); //移除所有"造成op(elem)结果为true"的元素
c.erase(pos);//移除pos上的元素,返回下一元素的位置
c.erase(beg,end);//移除...,返回下一元素的位置
c.resize(num);//将元素数量改为num
c.resize(num,elem);//将元素数量改为num(如果size()变大了,多出来的新元素都是elem的副本)
c.clear();


///
c.unique();
c.unique(op);
c1.splice(pos,c2);
c1.splice(pos,c2,c2pos);
c1.splice(pos,c2,c2beg,c2end);
c.sort();
c.sort(op);
c1.merge(c2);
c1.merge(c2,op);
c.reverse();//将所有元素反序

应用实例

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

void printLists(const list<int> &list1, const list<int> &list2)
{
	cout<<"list1 : ";
	copy(list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));

	cout<<"list2 : ";
	copy(list2.begin(), list2.end(), ostream_iterator<int>(cout, " "));
	cout<<endl<<endl;

}
int main(int argc, char **argv)
{
	//create two empty lists
	list<int> list1, list2;

	//fill boths lists with elements
	for (int i= 0 ; i < 6; ++ i) {
		list1.push_back(i);
		list2.push_back(i);
	}
  
	printLists(list1, list2);


	//move first element to the end
	list2.splice(list2.end(), list2, list2.begin());
	printLists(list1, list2);

	//sort second list, assign to list1 and remove duplicates
	list2.sort();
	list1 = list2;
	list2.unique();		
	printLists(list1, list2);

	//list1 merge with list2
	//then list2 is empty
	list1.merge(list2);
	printLists(list1, list2);

	list1.reverse();
	printLists(list1, list2);

	//filter repeate element, but first we need to sort
	list1.unique();
	printLists(list1, list2);

	return 0;
}
运行结果:



列表(list)是Python最常用的序列类型之一,它可以存储任意类型的数据,包括数字、字符串以及其他对象。列表是可变的,这意味着我们可以添加、删除或修改列表的元素。 以下是一些常见的列表操作: 1. 创建列表:可以使用方括号 [] 或 list() 函数来创建一个空的列表,例如: ```python my_list = [] my_list = list() ``` 我们也可以在创建列表时初始化它,例如: ```python my_list = [1, 2, 3, "hello", "world"] ``` 2. 访问列表元素:可以使用索引来访问列表的元素,例如: ```python my_list = [1, 2, 3, "hello", "world"] print(my_list[0]) # 输出: 1 print(my_list[3]) # 输出: hello ``` 3. 修改列表元素:可以使用索引来修改列表的元素,例如: ```python my_list = [1, 2, 3, "hello", "world"] my_list[0] = "hi" print(my_list) # 输出: ['hi', 2, 3, 'hello', 'world'] ``` 4. 列表切片:可以使用切片来访问列表的一个子集,例如: ```python my_list = [1, 2, 3, "hello", "world"] print(my_list[1:3]) # 输出: [2, 3] print(my_list[:3]) # 输出: [1, 2, 3] print(my_list[3:]) # 输出: ['hello', 'world'] ``` 5. 添加元素:可以使用 append() 方法向列表末尾添加一个元素,例如: ```python my_list = [1, 2, 3] my_list.append("hello") print(my_list) # 输出: [1, 2, 3, 'hello'] ``` 6. 插入元素:可以使用 insert() 方法在列表的指定位置插入一个元素,例如: ```python my_list = [1, 2, 3] my_list.insert(1, "hello") print(my_list) # 输出: [1, 'hello', 2, 3] ``` 7. 删除元素:可以使用 remove() 方法删除列表的指定元素,例如: ```python my_list = [1, 2, 3, "hello"] my_list.remove("hello") print(my_list) # 输出: [1, 2, 3] ``` 8. 获取元素索引:可以使用 index() 方法获取列表指定元素的索引,例如: ```python my_list = [1, 2, 3, "hello"] print(my_list.index("hello")) # 输出: 3 ``` 9. 统计元素出现次数:可以使用 count() 方法统计列表指定元素的出现次数,例如: ```python my_list = [1, 2, 3, "hello", "world", "hello"] print(my_list.count("hello")) # 输出: 2 ``` 10. 排序和反转:可以使用 sort() 方法对列表进行排序,reverse() 方法将列表反转,例如: ```python my_list = [3, 1, 2] my_list.sort() print(my_list) # 输出: [1, 2, 3] my_list.reverse() print(my_list) # 输出: [3, 2, 1] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值