C++ STL【list】(一)

1. list的介绍

list文档介绍
1 list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

2. list的使用

2.1 list的构造

构造函数( (constructor))接口说明
list()构造空的list
list (size_type n, const value_type& val = value_type())构造的list中包含n个值为val的元素
list (const list& x)拷贝构造函数
list (InputIterator first, InputIterator last)用[first, last)区间中的元素构造list
#include <iostream>
#include <list>
int main ()
{
	std::list<int> l1; // 构造空的l1
	std::list<int> l2 (4,100); // l2中放4个值为100的元素
	std::list<int> l3 (l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构	造l3
	std::list<int> l4 (l3); // 用l3拷贝构造l4
	// 以数组为迭代器区间构造l5
	int array[] = {16,2,77,29};
	std::list<int> l5 (array, array + sizeof(array) / sizeof(int) );
	// 用迭代器方式打印l5中的元素
	for(std::list<int>::iterator it = l5.begin(); it != l5.end(); it++)
	std::cout << *it << " ";
	std::cout<<endl;
	// C++11范围for的方式遍历
	for(auto& e : l5)
	std::cout<< e << " ";
	std::cout<<endl;
	return 0;
}

2.2 list iterator的使用

函数声明接口说明
begin +end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin +rend返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置

注意:

  1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
  2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt(16, 2);
	//正向迭代器遍历容器
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
		//反向迭代器遍历容器
	list<int>::reverse_iterator rit = lt.rbegin();
	while (rit != lt.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
	return 0;
}

2.3 list 大小

函数声明接口说明
empty检测list是否为空,是返回true,否则返回false
size返回list中有效节点的个数

2.4 list 访问

函数声明接口说明
front返回list的第一个节点中值的引用
back返回list的最后一个节点中值的引用

2.5 list 修改

函数声明接口说明
push_front在list首元素前插入值为val的元素
pop_front删除list中第一个元素
push_back在list尾部插入值为val的元素
pop_back删除list中最后一个元素
insert在list position 位置中插入值为val的元素
erase删除list position位置的元素
swap交换两个list中的元素
clear清空list中的有效元素
#include <list>
void PrintList(list<int>& l)
{
for (auto& e : l)
cout << e << " ";
cout << endl;
}
//=====================================================================================
====
// push_back/pop_back/push_front/pop_front
void TestList1()
{
	int array[] = { 1, 2, 3 };
	list<int> L(array, array+sizeof(array)/sizeof(array[0]));
	// 在list的尾部插入4,头部插入0
	L.push_back(4);
	L.push_front(0);
	PrintList(L);
	// 删除list尾部节点和头部节点
	L.pop_back();
	L.pop_front();
	PrintList(L);
}
//=========================================================================================
	// insert /erase
	void TestList3()
	{
	int array1[] = { 1, 2, 3 };
	list<int> L(array1, array1+sizeof(array1)/sizeof(array1[0]));
	// 获取链表中第二个节点
	auto pos = ++L.begin();
	cout << *pos << endl;
	// 在pos前插入值为4的元素
	L.insert(pos, 4);
	PrintList(L);
	// 在pos前插入5个值为5的元素
	L.insert(pos, 5, 5);
	PrintList(L);
	// 在pos前插入[v.begin(), v.end)区间中的元素
	vector<int> v{ 7, 8, 9 };
	L.insert(pos, v.begin(), v.end());
	PrintList(L);
	// 删除pos位置上的元素
	L.erase(pos);
	PrintList(L);
	// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
	L.erase(L.begin(), L.end());
	PrintList(L);
}
	// resize/swap/clear
	void TestList4()
	{
	// 用数组来构造list
	int array1[] = { 1, 2, 3 };
	list<int> l1(array1, 			array1+sizeof(array1)/sizeof(array1[0]));
	PrintList(l1);
	// 交换l1和l2中的元素
	l1.swap(l2);
	PrintList(l1);
	PrintList(l2);
	// 将l2中的元素清空
	l2.clear();
	cout<<l2.size()<<endl;
}

2.6 list 其他不常用函数

sort

(1)   void sort();

(2)	 template <class Compare>
  void sort (Compare comp);
  
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(7);
	lt.push_back(5);
	lt.push_back(6);
	lt.push_back(0);
	lt.push_back(1);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //7 5 6 0 1
	lt.sort(); //默认将容器内数据排为升序
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //1 0 5 6 7
	return 0;
}

splice
拼接链表

entire list (1)	
void splice (iterator position, list& x);
single element (2)	
void splice (iterator position, list& x, iterator i);
element range (3)	
void splice (iterator position, list& x, iterator first, iterator last);

splice函数用于两个list容器之间的拼接,其有三种拼接方式:

1、 将整个容器拼接到另一个容器的指定迭代器位置。
2、 将容器当中的某一个数据拼接到另一个容器的指定迭代器位置。
3、 将容器指定迭代器区间的数据拼接到另一个容器的指定迭代器位置。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt1(4, 2);
	list<int> lt2(4, 6);
	lt1.splice(lt1.end(), lt2); //将容器lt2拼接到容器lt1的末尾
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl; // 2 2 2 6 6 6 6 2

	list<int> lt3(4, 2);
	list<int> lt4(4, 6);
	lt3.splice(lt3.begin(), lt4, lt4.begin()); //将容器lt4的第一个数据拼接到容器lt3的开头
	for (auto e : lt3)
	{
		cout << e << " ";
	}
	cout << endl; //6 2 2 2 2 

	list<int> lt5(4, 2);
	list<int> lt6(4, 6);
	lt5.splice(lt5.end(), lt6, lt6.begin(), lt6.end()); //将容器lt6的指定迭代器区间内的数据拼接到容器lt5的末尾
	for (auto e : lt5)
	{
		cout << e << " ";
	}
	cout << endl; // 2 2 2 2 6 6 6 6
	return 0;
}

remove
删除list中所有为val值的节点。

void remove (const value_type& val);
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(4);
	lt.push_back(3);
	lt.push_back(3);
	lt.push_back(2);
	lt.push_back(2);
	lt.push_back(3);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //1 4 3 3 2 2 3
	lt.remove(3); //删除容器当中值为3的元素
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //1 4 2 2
	return 0;
}

remove_if

remove_if函数用于删除容器当中满足条件的元素。

template <class Predicate>
  void remove_if (Predicate pred);
#include <iostream>
#include <list>
using namespace std;

bool single_digit(const int& val)
{
	return val < 10;
}
// 仿函数
struct is_odd {
  bool operator() (const int& val) { return val<10 }
};
int main()
{
	list<int> lt;
	lt.push_back(10);
	lt.push_back(4);
	lt.push_back(7);
	lt.push_back(18);
	lt.push_back(2);
	lt.push_back(5);
	lt.push_back(9);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //10 4 7 18 2 5 9
	//lt.remove_if(is_odd );
	lt.remove_if(single_digit); //删除容器当中值小于10的元素
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //10 18
	return 0;
}

unique
unique函数用于删除容器当中连续的重复元素。

(1)	
void unique();
(2)	
template <class BinaryPredicate>
  void unique (BinaryPredicate binary_pred);
// list::unique
#include <iostream>
#include <cmath>
#include <list>

// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }

// a binary predicate implemented as a class:
struct is_near {
  bool operator() (double first, double second)
  { return (fabs(first-second)<5.0); }
};

int main ()
{
  double mydoubles[]={ 12.15,  2.72, 73.0,  12.77,  3.14,
                       12.77, 73.35, 72.25, 15.3,  72.25 };
  std::list<double> mylist (mydoubles,mydoubles+10);
  
  mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
                             // 15.3,  72.25, 72.25, 73.0,  73.35

  mylist.unique();           //  2.72,  3.14, 12.15, 12.77
                             // 15.3,  72.25, 73.0,  73.35

  mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15
                                       // 15.3,  72.25, 73.0

  mylist.unique (is_near());           //  2.72, 12.15, 72.25

  std::cout << "mylist contains:";
  for (std::list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

注意: 若想使用unique函数做到真正的去重,还需在去重前对容器内元素进行排序。

merge
merge函数用于将一个有序list容器合并到另一个有序list容器当中,使得合并后的list容器任然有序。(类似于归并排序)

(1)	
  void merge (list& x);
(2)	
template <class Compare>
  void merge (list& x, Compare comp);
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt1;
	lt1.push_back(3);
	lt1.push_back(8);
	lt1.push_back(1);
	list<int> lt2;
	lt2.push_back(6);
	lt2.push_back(2);
	lt2.push_back(9);
	lt2.push_back(5);
	lt1.sort(); //将容器lt1排为升序
	lt2.sort(); //将容器lt2排为升序
	lt1.merge(lt2); //将lt2合并到lt1当中
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl; //1 2 3 5 6 8 9 
	return 0;
}

reverse
reverse函数用于将容器当中元素的位置进行逆置。

void reverse();
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.reverse(); //将容器当中元素的位置进行逆置
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //5 4 3 2 1 
	return 0;
}

assign
将原有的所有元素覆盖。

range (1)	
template <class InputIterator>
  void assign (InputIterator first, InputIterator last);
fill (2)	
void assign (size_type n, const value_type& val);
#include <iostream>
#include <string>
#include <list>
using namespace std;

int main()
{
	list<char> lt(3, 'a');
	lt.assign(3, 'b'); //将新内容分配给容器,替换其当前内容
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //b b b
	string s("hello world");
	lt.assign(s.begin(), s.end()); //将新内容分配给容器,替换其当前内容
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //h e l l o   w o r l d
	return 0;
}

swap
swap函数用于交换两个容器的内容。

void swap (list& x);
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt1(4, 2);
	list<int> lt2(4, 6);
	lt1.swap(lt2); //交换两个容器的内容
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl; //6 6 6 6
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl; //2 2 2 2
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2023框框

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值