C++ 基础专题容器(list)

前言

        本文主要是总结常用容器,加深理解以及实际使用。相关完整网站参考:C++函数和容器网站

本文主要是关注C++11中的定义和用法。

list

一、类和定义

        template < class T, class Alloc = allocator<T> > class list;

List containers are implemented as doubly-linked lists。(双向链表为list的底层原理)

Compared to other base standard sequence containers (arrayvector and deque), lists perform generally better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms。(与其他基本标准序列容器(array、vector和deque)相比,列表在容器内的任何位置插入、提取和移动元素(迭代器已经获得)方面通常表现更好,因此在大量使用列表的算法(如排序算法)中也表现更好。)

        list优点是方便插入、访问和移动,缺点是访问指定的某个位置直接访问元素。

1.1 特性

        线性顺序排列;双向链表可以知道前序和后序,但不支持随机访问;动态申请内存。

1.2 定义

二、 使用

2.1 成员函数

构造函数和使用

default (1)	
explicit list (const allocator_type& alloc = allocator_type());
fill (2)	
explicit list (size_type n);         list (size_type n, const value_type& val,                const allocator_type& alloc = allocator_type());
range (3)	
template <class InputIterator>  list (InputIterator first, InputIterator last,         const allocator_type& alloc = allocator_type());
copy (4)	
list (const list& x);list (const list& x, const allocator_type& alloc);
move (5)	
list (list&& x);list (list&& x, const allocator_type& alloc);
initializer list (6)	
list (initializer_list<value_type> il,       const allocator_type& alloc = allocator_type());
//list新建对象
void ListConstructor()
{
	list<int> list_a;
	list<int> list_b(4, 100);
	list<int> list_c(list_b.begin(), list_b.end());
	list<int> list_d(list_c);

	//使用数组初始化
	int arry_a[] = { 1,2,3,4 };
	list<int> list_e(arry_a,arry_a+sizeof(arry_a)/sizeof(int));
	cout << "list中的值"<<endl;
	for (list<int>::iterator it = list_e.begin(); it != list_e.end(); it++)
	{
		cout << *it << endl;
	}


};

1.2 析构

~list();

1.3 元素获取 (特别注意)

        只有front和back

1.4 修改

void ListModify()
{
	//	assign,emplace_front,push_front,pop_front,emplace_back,push_back,pop_back,emplace,insert,erase,swap,resize,clear
	// insert和emplace的比较:insert可以插多个元素,emplace只能插一个;emplace更高效,推荐使用。
	//-----------------assign----------------------------begin--
	list<int> list_assign1;
	list<int> list_assign2;
	list_assign1.assign(7, 100);
	list_assign2.assign(list_assign1.begin(), list_assign1.end());
	//-----------------assign----------------------------end--

	//-----------------emplace_front--------------------------
	list<pair<int, char>> list_emplace1;
	list_emplace1.emplace_front(1, 'a');
	list_emplace1.emplace_front(2, 'b');
	list_emplace1.emplace_front(3, 'c');
	//result:(3,c) (2,b) (1,a)
	//-----------------emplace_front--------------------------

	//-----------------emplace_back------------------------
	list<pair<int, char>> list_back1;
	list_back1.emplace_back(1, 'a');
	list_back1.emplace_back(2, 'b');
	list_back1.emplace_back(3, 'c');
	//result:(1,a) (2,b) (3,c)
	//-----------------emplace_back--------------------------

	//-----------------emplace------------------------
	list<pair<int, char>> list_emp1;
	list_emp1.emplace(list_emp1.begin(),1, 'a');
	list_emp1.emplace(list_emp1.begin(),2, 'b');
	//result:(2,b) (1,a) 
	//-----------------emplace--------------------------

	//-----------------push_front------------------------
	list<int> list_push_front1(2,3);
	list_push_front1.push_front(4);
	list_push_front1.push_front(5);
	for (list<int>::iterator it = list_push_front1.begin(); it != list_push_front1.end(); it++)
	{
		cout << *it << endl;
	}
	//result:5  4  3  3
	//-----------------push_front--------------------------


}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值