C++——List容器使用总结

一丶List容器介绍

List是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针实现的。

链表的组成: 链表是由一系列结点组成的。

链表的组成: 一个是存放数据的数据域,另一个是存放下一个结点地点的指针域。

STL中的链表是双向链表
在这里插入图片描述

二丶List容器的构造函数

函数原型

list<type> listName;		//默认的构造函数
list(begin,end); 			//List容器在这段区间内拷贝给新函数
list(n,elem);				//用n个elem填充list容器
list(const list& li);		//拷贝构造函数

案例:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{	
		cout << *li << " " ;

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	cout << "L1为: ";
	printList(L1);

	list<int>L2(L1.begin(), (--L1.end()));
	cout << "L2为: ";
	printList(L2);


	list<int>L3(5, 100);
	cout << "L3为: ";
	printList(L3);


	list<int>L4(L2);
	cout << "L4为: ";
	printList(L4);
}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述

三丶List赋值和交换

函数原型

asign(begin,end); 			//List容器在这段区间内拷贝给本身
asign(n,elem);				//用n个elem填充list容器
list& operator=(const list& li);		//重载操作符=
swap(li);					//自身与li交换

案例:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{	
		cout << *li << " " ;

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	cout << "L1为: ";
	printList(L1);

	list<int>L2;
	L2.assign(L1.begin(), (--L1.end()));
	cout << "L2为: ";
	printList(L2);


	list<int>L3;
	L3.assign(5, 100);
	cout << "L3为: ";
	printList(L3);

//一下两种方式都可以
//	list<int>L4 = L2;
	list<int>L4;
	L4 = L2;
	cout << "L4为: ";
	printList(L4);

	cout << "***********************************" << endl;
	cout << "交换L3和L4后" << endl;
	L3.swap(L4);
	cout << "L3为: ";
	printList(L3);
	cout << "L4为: ";
	printList(L4);
}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述

四丶List大小操作

函数原型

size();				//返回容器的大小
empty();			//返回容器是否为空
resize(n);			//重新分配容器大小,如果重新分配大的话用默认值填补,如果小的话截取
resize(n,elem);		//重新分配容器大小,如果重新分配大的话用elem值填补,如果小的话截取

案例:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{	
		cout << *li << " " ;

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	cout << "L1为: ";
	printList(L1);

	list<int>L2;
	L2.assign(L1.begin(), (--L1.end()));
	cout << "L2为: ";
	printList(L2);


	list<int>L3;
	L3.assign(5, 100);
	cout << "L3为: ";
	printList(L3);
	cout << "L3的大小为:" << L3.size() << endl;
	if (L3.empty()){
		cout << "L3为空" << endl;

		}
	else
	{
		cout << "L3不为空" << endl;
	}
//一下两种方式都可以
//	list<int>L4 = L2;
	list<int>L4;
	L4 = L2;
	cout << "L4为: ";
	printList(L4);
	cout << "L4的大小为:" << L4.size() << endl;
	L4.resize(10);
	cout << "L4重新分配大小之后为:" << endl;
	printList(L4);
	L4.resize(13,2);
	cout << "L4第二次重新分配大小之后为:" << endl;
	printList(L4);

}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述

五丶List插入和删除

函数原型

push_back(elem);			//从容器的尾部增加一个元素elem
pop_back();					//从容器的尾部移除第一个元素
push_front(elem);			//从容器的头部增加一个元素elem
pop_front();				//从容器的头部移除第一个元素
insert(pos,elem);			//在pos位置插入elem元素
insert(pos,n,elem);			//在pos位置插入n个elem个元素
insert(pos,beg,end);		//在pos位置插入[beg,end)区间的元素
clear();					//清空List容器的元素
erase(beg,end);				//删除从beg到end位置的元素
erase(pos);					//删除pos位置的元素,返回下一个元素的位置
remove(elem);				//删除容器中所有跟elem值匹配的成员

案例:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{
		cout << *li << " ";

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	
	//push_back(elem);			//从容器的尾部增加一个元素elem
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_back(60);
	L1.push_back(70);
	L1.push_back(80);
	cout << "L1为: ";
	printList(L1);
	
	//push_front(elem);			//从容器的头部增加一个元素elem
	L1.push_front(10);
	cout << "L1为: ";
	printList(L1);
	
	//pop_back();					//从容器的尾部移除第一个元素
	L1.pop_back();
	cout << "L1为: ";
	printList(L1);
	
	//pop_front();				//从容器的头部移除第一个元素
	L1.pop_front();
	cout << "L1为: ";
	printList(L1);
	
	//insert(pos,elem);			//在pos位置插入elem元素
	L1.insert(L1.begin(), 5);
	cout << "L1为: ";
	printList(L1);
	
	//insert(pos,n,elem);			//在pos位置插入n个elem个元素
	L1.insert((++L1.begin()), 5,3);
	cout << "L1为: ";
	printList(L1);
	
	list<int>L3;
	L3.assign(5, 100);
	//insert(pos,beg,end);		//在pos位置插入[beg,end)区间的元素
	L1.insert(L1.begin(), L3.begin(), L3.end());
	cout << "L1为: ";
	printList(L1);
	
	//erase(beg,end);				//删除从beg到end位置的元素
	L1.erase(L1.begin(), ++(++L1.begin()));
	cout << "L1为: ";
	printList(L1);
	
	//erase(pos);					//删除pos位置的元素,返回下一个元素的位置
	L1.erase(L1.begin());
	cout << "L1为: ";
	printList(L1);
	
	//remove(elem);				//删除容器中所有跟elem值匹配的成员
	L1.remove(3);
	cout << "L1为: ";
	printList(L1);
	
	//clear();					//清空List容器的元素
	L1.clear();
	cout << "L1为: ";
	printList(L1);

}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述

六丶List数据存取

函数原型

front();	//返回第一个元素
back();		//返回最后一个元素

实例为:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{
		cout << *li << " ";

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_back(60);
	L1.push_back(70);
	L1.push_back(80);
	cout << "L1为: ";
	printList(L1);
	cout << "L1的第一个元素为:\t" << L1.front() << endl;
	cout << "L1的最后一个元素为:\t" << L1.back() << endl;

}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述

七丶List反转和排序

函数原型

reverse();			//反转链表
sort();				//对链表排序

实例为:

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
	for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
	{
		cout << *li << " ";

	}
	cout << endl;
}
void test01(){
	list<int> L1;
	L1.push_back(90);
	L1.push_back(80);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_back(60);
	L1.push_back(70);
	L1.push_back(20);
	cout << "L1为: \t\t";
	printList(L1);
	L1.reverse();
	cout << "L1反转之后为:\t";
	printList(L1);
	L1.sort();
	cout << "L1排序之后为:\t";
	printList(L1);
}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:
在这里插入图片描述


博主学习的视频链接:(感觉讲得很好)
https://www.bilibili.com/video/av41559729?p=217

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用`std::string`类来填充字符串。根据引用中的示例代码,可以使用`<<`运算符将字符串添加到`std::stringstream`对象中,然后使用`str()`函数将其转换为字符串。另外,根据引用中提供的不同的`assign`函数重载,可以使用不同的方式来填充`std::string`对象。 例如,可以使用`assign`函数将一个C类型字符串赋值给`std::string`对象,如下所示: ``` std::string str; const char* cStr = "Hello"; str.assign(cStr); ``` 这将把"C风格"字符串"Hello"赋值给`str`。如果需要指定字符串的长度,可以使用带有`int`参数的`assign`函数: ``` const char* cStr = "Hello, World!"; int length = 5; str.assign(cStr, length); ``` 这将把"C风格"字符串"Hello, World!"的前5个字符赋值给`str`。 此外,还可以使用`+=`运算符将另一个`std::string`对象连接到已有的字符串末尾: ``` std::string str = "Hello"; std::string appendStr = " World!"; str += appendStr; ``` 这将把字符串" World!"连接到`str`的末尾,结果为"Hello World!"。 总之,可以根据需要使用`assign`函数或`+=`运算符来填充`std::string`对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c++ 之字符串补充篇](https://blog.csdn.net/u010092716/article/details/104341789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [标准C++中string类](https://blog.csdn.net/jiabingxi/article/details/17321331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值