容器:list用法及示例

list用法及示例

一、简介

list 容器,又称双向链表容器。
该容器的底层是以双向链表的形式实现的

二、容器特性

list 容器中各个元素的前后顺序是靠指针来维系的,每个元素都配备了 2 个指针,分别指向它的前一个元素和后一个元素。其中第一个元素的前向指针总为 null,尾部元素的后向指针也总为 null。

它可以在序列已知的任何位置快速插入或删除元素(时间复杂度为O(1))。并且在 list 容器中移动元素,也比其它容器的效率高。

它不能像 array 和 vector 那样,通过位置直接访问元素。

只有运用迭代器,才能访问 list 容器中存储的各个元素。

注意:

  • 双向迭代器不能通过下标访问 list 容器中指定位置处的元素。
  • 双向迭代器不支持使用 -=、+=、+、- 运算符。
  • 双向迭代器不支持使用 <、 >、 <=、 >= 比较运算符。

三、使用

成员函数

成员函数 功能
begin() 返回指向容器中第一个元素的双向迭代器。
end() 返回指向容器中最后一个元素所在位置的下一个位置的双向迭代器。
rbegin() 返回指向最后一个元素的反向双向迭代器。
rend() 返回指向第一个元素所在位置前一个位置的反向双向迭代器。
cbegin() 和 begin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
cend() 和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
crbegin() 和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
crend() 和 rend() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
empty() 判断容器中是否有元素,若无元素,则返回 true;反之,返回 false。
size() 返回当前容器实际包含的元素个数。
max_size() 返回容器所能包含元素个数的最大值。一般是 2的32次方-1 或者2的62次方-1。
front() 返回第一个元素的引用。
back() 返回最后一个元素的引用。
assign() 用新元素替换容器中原有内容。
emplace_front() 在容器头部生成一个元素。该函数和 push_front() 的功能相同,但效率更高。
push_front() 在容器头部插入一个元素。
pop_front() 删除容器头部的一个元素。
emplace_back() 在容器尾部直接生成一个元素。该函数和 push_back() 的功能相同,但效率更高。
push_back() 在容器尾部插入一个元素。
pop_back() 删除容器尾部的一个元素。
emplace() 在容器中的指定位置插入元素。该函数和 insert() 功能相同,但效率更高。
insert() 在容器中的指定位置插入元素。
erase() 删除容器中一个或某区域内的元素。
swap() 交换两个容器中的元素,必须保证这两个容器中存储的元素类型是相同的。
resize() 调整容器的大小。
clear() 删除容器存储的所有元素。
splice() 将一个 list 容器中的元素插入到另一个容器的指定位置。
remove(val) 删除容器中所有等于 val 的元素。
remove_if() 删除容器中满足条件的元素。
unique() 删除容器中相邻的重复元素,只保留一个。
merge() 合并两个事先已排好序的 list 容器,并且合并之后的 list 容器依然是有序的。
sort() 通过更改容器中元素的位置,将它们进行排序。
reverse() 反转容器中元素的顺序。

几种创建方式

  1. 创建一个没有任何元素的空list容器:
std::list<int> test;
  1. 创建一个包含n个元素的list容器:
std::list<int> test(10);
  1. 创建一个包含n个元素的list容器,并赋初始值:
std::list<int> test(10, 9);
std::list<int>test{ 1,2,3,4,5,6,7,8,9 };
  1. 通过拷贝一个已有的容器的方式创建新的list容器:
std::list<int> tmp(10);
std::list<int> test(tmp);
  1. 通过拷贝其他类型容器(或数组)中指定区域内的元素的方式创建list容器:
int a[] = { 1,2,3,4,5,6,7,8,9 };
std::list<int> test(a, a+9);
std::array<int, 9>arr{ 1,2,3,4,5,6,7,8,9 };
std::list<int>test(arr.begin()+2,arr.end()-4);//拷贝arr容器中的{3,4,5}

示例:

demo1:

#include <iostream>
#include <list>
int main()
{
	std::array<int, 9>arr{ 1,2,3,4,5,6,7,8,9 };
	std::list<int>test(arr.begin(), arr.end());//通过拷贝其他类型容器(或数组)中指定区域内的元素的方式创建list容器
	test.emplace_front(0);//头部插入一个元素
	test.emplace_back(-1);//尾部插入一个元素
	test.remove(9);//移除9这个元素
	for (auto &i : test) {//修改指定元素
		if (i == 5) {
			i = 555;
		}
	}
	for (auto i : test) {
		printf(" %d", i);
	}
	printf("n排序n");
	test.sort();//排序
	for (auto i : test) {
		printf(" %d", i);
	}
	printf("n");
}

结果:

 0 1 2 3 4 555 6 7 8 -1
排序
 -1 0 1 2 3 4 6 7 8 555

demo2: 插入:push_front(), push_back(), emplace_front(), emplace_back(), emplace()

#include <iostream>
#include <list>
int main()
{
	std::list<int> test{ 1,2,3 };
	test.push_front(0);			//头部插入一个元素{0,1,2,3}
	test.push_back(4);			//尾部插入一个元素{0,1,2,3,4}
	test.emplace_front(-1);		//头部插入一个元素{-1,0,1,2,3,4}
	test.emplace_back(5);		//尾部插入一个元素{-1,0,1,2,3,4,5}
	test.emplace(test.end(), 6);//向指定位置插入一个元素{-1,0,1,2,3,4,5,6}
	for (auto i : test) {
		std::cout << i << " ";
	}
}
-1 0 1 2 3 4 5 6

demo3: 插入:insert()的多种方式

#include <iostream>
#include <list>
int main()
{
	std::list<int> test{ 1,2 };
	test.insert(test.begin(), 0);//指定的位置之前插入 1 个元素 3 {0,1,2}
	test.insert(test.end(), 2, 3);//指定的位置之前插入 2 个元素 3 {0,1,2,3,3}
	std::array<int, 3>tmp{ 4,5,6 };
	test.insert(test.end(), tmp.begin(), tmp.end());//将其他容器中特定位置的元素插入指定位置 {0,1,2,3,3,4,5,6}
	test.insert(test.end(),{7,8});//插入初始化列表中所有的元素{0,1,2,3,3,4,5,6,7,8}
	for (auto i : test) {
		std::cout << i << " ";
	}
}
0 1 2 3 3 4 5 6 7 8

demo4: 移动:splice()的多种方式

#include <iostream>
#include <list>
int main()
{
	std::list<int> test1{ 1,2,3,4 };
	std::list<int> test2{ 5,6,7 };
	std::list<int>::iterator it = ++test1.begin(); //it 迭代器指向元素 2

	//第一种用法:将test2的所有元素移动到2所在的位置:
	test1.splice(it, test2);//test1:{1,5,6,7,2,3,4}; test2:{}; it迭代器仍然指向元素2
													
	//第二种用法:将 it 指向的元素 2 移动到 test2.begin()位置:
	test2.splice(test2.begin(), test1, it);	//test1:{1,5,6,7,3,4}; test2:{2}; it仍然指向元素2

	//第三种用法:,将 [test1.begin(),test1.end())范围内的元素移动到 test2.begin() 位置:
	test2.splice(test2.begin(), test1, test1.begin(), test1.end());	//test1:{}; test2:{1,5,6,7,3,4,2};it仍然指向元素2
																
	std::cout << "test1.size(): " << test1.size() << std::endl;
	std::cout << "test2.size(): " << test2.size() << std::endl;
	std::cout << "it: " << *it << std::endl;
	for (auto i : test2) {
		std::cout << i << " ";
	}
}
test1.size(): 0
test2.size(): 7
it: 2
1 5 6 7 3 4 2

demo5: 删除pop_back();pop_front();erase();remove();clear()

#include <iostream>
#include <list>
int main()
{
	std::list<int> test{ 0,1,2,3,3,3,4,5,6,7,6,5,4,3,2,1,99,0 };
	test.pop_back();//移除最后一个元素{0,1,2,3,3,3,4,5,6,7,6,5,4,3,2,1,99}
	test.pop_front();//移除第一个元素{1,2,3,3,3,4,5,6,7,6,5,4,3,2,1,99}
	test.erase(++test.begin()); //删除第二个元素{1,3,3,3,4,5,6,7,6,5,4,3,2,1,99}
	//删除指定元素
	for (std::list<int>::iterator i = test.begin(); i != test.end();++i) {
		if ((*i) == 7) {
			test.erase(i);
		}
	}
	//{1,3,3,3,4,5,6,6,5,4,3,2,1,99}
	test.remove(99);//删除元素99 {1,3,3,3,4,5,6,6,5,4,3,2,1}
	
	for (auto i : test) {
		std::cout << i << " ";
	}
	std::cout<< std::endl;
	test.erase(++test.begin(), --test.end());//删除指定一段元素 {1,1}
	for (auto i : test) {
		std::cout << i << " ";
	}
	test.clear();//清空
}
1 3 3 3 4 5 6 6 5 4 3 2 1
1 1

demo6 :删除相邻重复的元素 unique()的两种方式

#include <iostream>
#include <list>
//二元谓词函数
bool fuc1(int a, int b) {
	if (a / 10 == b / 10) {
		return true;
	}
	return false;
}

int main()
{
	std::list<int> test{10,20,30,30,31,32,33,40,50,60,30,50,60};
	//方式一:
	test.unique();//删除相邻重复的元素,仅保留一份
	for (auto i : test) {
		std::cout << i << " ";
	}
	std::cout << std::endl;
	//方式二:
	test.unique(fuc1);//fuc1 为二元谓词函数,是我们自定义的去重规则
	for (auto i : test) {
		std::cout << i << " ";
	}
	std::cout << std::endl;
}
10 20 30 31 32 33 40 50 60 30 50 60
10 20 30 40 50 60 30 50 60

demo7: 删除能够使lamba表达式成立的所有元素 remove_if()

#include <iostream>
#include <list>
int main()
{
	std::list<int> test{1,2,3,4,5,6,7,8,9,10};
	test.remove_if([](int n){return (n % 2 != 0);} ); //删除test容器中能够使lamba表达式成立的所有元素。
	for (auto i : test) {
		std::cout << i << " ";
	}
	std::cout << std::endl;
}
2 4 6 8 10

demo8: 可使用std::advance()更便捷的移动迭代器(注意不要越界)

#include <iostream>
#include <list>
int main()
{
	std::list<int> test{1,2,3,4,5,6,7,8,9,10};
	auto it = test.begin();
	std::advance(it, 5);
	*it = 60;
	for (auto i : test) {
		std::cout << i << " ";
	}
	std::cout << std::endl;
}
1 2 3 4 5 60 7 8 9 10

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>