百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

C++ string/stack/list/deque/multiset 小结

liuian 2025-07-07 20:08 45 浏览


1.string基础操作(包括初始化,遍历,替换,增删改查等基础操作)

2.stack基础操作(包括出栈,入栈,栈顶元素的获取和弹出)

3.list 基础操作(如何高效地进行插入删除操作)

4.deque基础操作(排序和查找)

5.multiset基础操作(解决了set不能同一个值出现的问题)


示例代码1 string基础操作

#include<iostream>
using namespace std;

void main11()
{
	//初始化方式
	string s1 = "aaaa";
	string s2("aaaa");
	string s3 = s2;
	string s4 (10, 'a');
	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;
	cout << "s3:" << s3 << endl;
	cout << "s4:" << s4 << endl;


}
//string 的遍历
void main22()
{
	//1  数组方式
	string s1 = "asddfff";
	for (int i = 0; i < s1.length(); i++)
	{
		cout << a[i] << "";
	}
	//2  迭代器
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << "";

	}
	cout << endl;
	for (int i = 0; i < s1.length(); i++)
	{
		cout << s1.at(i) << "";//抛出异常
	}
	cout << endl;

}
//字符指针和string 转换
void main33()
{

	string s1 = "aaa";
	printf("s1:%s\n", s1.c_str());
	//s1的内容拷贝到buf中
	char buf1[128];//分配内存
	s1.copy(buf1, 3, 0);//只会拷贝三个字符,不会变成c风格的字符串
	cout << "buf1:" << buf1 << endl;
}
//连接字符串
void main44()
{
	string s1 = "aaa";
	string s2 = "bbb";
	s1 = s1 + s2;
	cout << s1 << endl;
	string s3 = "999";
	string s4 = "4444";
	s3.append(s4);
	cout << "s3:" << s3 << endl;
}
//字符串的查找和替换
void main55()
{

	string s1 = "sdf 555 sdgdf4564dfgtysrtw4tgrsghtyju7io869i7eu5ewtgrs";
	int index = s1.find("555", 0);
	cout << "index:" << index << endl;//位置下标,从0开始
	int offindex = s1.find("555", 0);
	while (offindex != string::npos)//出现的次数,数组下标
	{

		cout << "offindex" << offindex << endl;
		offindex = offindex + 1;
		s1.find("555", offindex);
	}
	//案例2 替换
	string s3 = "aaa  bbb  ccc";
	s3.replace(0, 3, "AAA");
	cout << "s3" << s3 << endl;



	 offindex = s1.find("555", 0);
	while (offindex != string::npos)//出现的次数,数组下标
	{

		cout << "offindex" << offindex << endl;
		s1.replace(offindex, 3, "555");
		offindex = offindex + 1;
		s1.find("555", offindex);
	}
	cout << "s1替换后的结果:" << s1 << endl;
}
//区间删除和插入
void main66()

{

	string s1 = "hello1  hello2 hello1";
	string::iterator it = find(s1.begin(), s1.end(), '1');
	if (it != s1.end())
	{
		s1.erase(it);
	}
	cout << "s1删除以后的结果" <<s1<< endl;
	s1.erase(s1.begin(), s1.end());
	cout << "s1全部删除:" << s1 << endl;
	cout << "s1长度:" << s1.length() << endl;

}
void main77()
{

	string s1 = "AAAbbb";
	//函数的入口地址 函数对象 预定义的函数对象
	transform(s1.begin(), s1.end(), s1.begin() ,tuopper);
	cout << s1 << endl;
	string s2 = "AAAbbb";
	transform(s1.begin(), s1.end(), s2.begin(), tulower);
	cout << "s2" << s2 << endl;
}
	void main()
{
		main11();
		main22();
		main33();
		main44();
		main55();
		main66();

	cout << "hello..." << endl;
	system("pause");
	return;
}

示例代码2 stack基础操作

#include<iostream>
using namespace std;
#include"stack"
//栈的模型
void main11()
{

	stack<int> s;
	//入栈
	for (int i = 0; i < 10; i++)
	{
		
		//出栈
	while (!s.empty())
	{
		int tmp = s.top();//获取栈顶元素
		cout << tmp << "";
		s.top();//弹出栈顶元素

	}
}
	class Teacher
	{
	public:
		int age;
		char name[32];
	public:
		void printT()
		{
			cout << "age:" << age << endl;
		}



	};

void main22()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	stack<Teacher> s;
	s.push(t1);
	s.push(t2);
	s.push(t3);
	while (!s.empty())
	{
		Teacher tmp = s.top();//获取栈顶元素
		tmp.printT();

		s.top();//弹出栈顶元素

	}

void main33()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	stack<Teacher*> s;
	s.push(&t1);
	s.push(&t2);
	s.push(&t3);
	while (!s.empty())
	{
		Teacher *p = s.top();//获取栈顶元素
		p->printT();

		s.top();//弹出栈顶元素

	}
void main()
{
	main11();
	main22();
	main33();
	cout << "hello..." << endl;
	system("pause");
	return;
}

示例代码 3 list 基础操作

/*
1 list是一个双向列表容器,可以高效地进行插入删除操作(左闭右开)
2 list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符
#include<list>



*/
#include<iostream>
using namespace std;
#include"list"
void mian11()
{

	list < int> l;
	cout << "list的大小" << l.size() << endl;//测试
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);//尾插法

	}
	cout << "" << l.size() << endl;

	list<int>::iterator it = l.begin();//迭代器
	while (it != l.end())
	{
		cout << *it << "";
		it++;
	}
	it = l.begin();//list不能随机访问
	it++;
	it++;
	it++;
	list.insert(it, 100);//100插入在哪里?
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << endl;
	}
	//结论:链表的结点index序号是从0号位置开始的.在3号位置插入元素,原来的三号位置,变成4号位置.
	
}

void main33()
{
	list < int> l;
	cout << "list的大小" << l.size() << endl;//测试
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);//尾插法

	}
	cout << "" << l.size() << endl;
	list<int>::iterator it1 = l.begin();
	list<int>::iterator it2 = l.begin();
	it2++;
	it2++;
	it2++;
	l.erase(it1, it2);
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << endl;
	}
	cout << endl;
	//插入和删除
	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	l.erase(l.begin());
	l.remove(100);
}
	void main()
	{
		//main11();

		///main33();
		cout << "hello..." << endl;
		system("pause");
		return;
	}

示例代码4 deque基础操作

#include<iostream>
using namespace std;
#include"deque"
#include"algorithm"
void printD(deque<int>&d)
{
	for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << endl;
	}
}
void main11()
{
	deque<int> d1;
	d1.push_back(1);
	d1.push_back(3);
	d1.push_back(5);
	d1.push_front(-11);
	d1.push_front(-33);
	d1.push_front(-55);
	cout << "头部元素" << d1.front << endl;
	cout << "尾部元素" <<d1.back<< endl;
	printD(d1);

	d1.pop_front();
	d1.pop_back();
	printD(d1);
	//查找-33数组下标的值
	deque<int>::iterator it = find(d1.begin(), d1.end(), -33);
	if (it != d1.end())
	{

		cout << "-33数组下标是" << distance(d1.begin(), it) << endl;

	}
	else
	{
		cout << "没有找到值为-33的元素" << endl;
	}

}
void main()
{
	main11();
	cout << "hello..." << endl;
	system("pause");
	return;
}

示例代码5multiset基础操作

#include<iostream>
using namespace std;
#include<queue>
#include"list"
#include"set"
void main11()
{

	multiset<int> setl;
	int tmp = 0;

	printf("请输入multiset集合的值:");
	scanf("&d", &tmp);
	while (tmp != 0)
	{
		setl.insert(tmp);
		printf("请输入multiset集合的值:");
		scanf("&d", &tmp);
	}
	for (multiset<int>::iterator it = setl.begin(); it != setl.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	while (!setl.empty())
	{
		multiset<int>::iterator it = setl.begin();
		cout << *it << " ";
		setl.erase(it);
	}
	
}
void main()
{
	main11();


	cout << "hello..." << endl;
	system("pause");
	return;
}

予人玫瑰,手有余香.小羊伴你一路同行~~~

相关推荐

推特官网入口(推特官网入口网页登录网址)

首先在浏览器中打开推特的官网,然后点击页面上的“注册”创建账号。Twitter可以让用户更新不超过140个字符的消息(除中文、日文和韩语外已提高上限至280个字符),这些消息也被称作“推文(Tweet...

windows7如何清理c盘(win7怎么清理c盘)

1.打开桌面计算机,右键点击“C盘”,并选取“属性”。2.待新窗口弹出后,依次点击“工具”、“立即进行碎片整理”。3.最后,选取C盘,在按下“磁盘碎片整理”按钮,系统就会对C盘进行分析,并进行整理。4...

win10自带分区工具(win10官方分区工具)

Win10自带的分区工具是磁盘管理器,可以用来创建、删除、格式化和调整磁盘分区。下面是使用磁盘管理器分区的步骤:1.打开磁盘管理器。您可以在Windows10搜索栏中输入“磁盘管理器”来快速打开。...

appstore正版下载软件(apple store下载正版)

不会,他是正版的,因为只有ios系统可以用,但他里面的好游戏都是要收费的,所以打架都要越狱,去其它地方下载,不去商店的在安卓上,GooglePlayStore是类似于苹果的AppStore一...

手机锁屏密码键盘没了(手机输入密码的键盘没了怎么办)

如果手机锁屏密码的键盘找不到,首先要确认是否是由于软件问题导致的。可以尝试重启手机或者清理手机缓存来解决。如果问题仍然存在,可以尝试更换输入法或者恢复手机出厂设置来解决。如果以上方法都没有效果,建议联...

移动硬盘跟固态硬盘的区别(移动硬盘跟固态硬盘的区别是什么)

一:移动硬盘移动硬盘是指以传统机械磁盘作为存储介质,用于计算机之间交换大容量数据,讲究移动便携性的存储产品。优点:具有容量大、价格便宜的特点,方便存储大量文件数据。(推荐学习:web前端视频教程)缺...

windows怎么截图快捷键(windows截图快捷键没反应)

1、按Prtsc键截图这样获取的是整个电脑屏幕的内容,按Prtsc键后,可以直接打开画图工具,接粘贴使用。也可以粘贴在QQ聊天框或者Word文档中,之后再选择保存即可。2、按Ctrl+Prtsc键截图...

显示器分辨率有哪几种(显示器屏幕分辨率都有哪些)

目前使用较多的显示器分辨率有640*480,800*600,1024*768,1280*1024四种。刷新率,这主要是指显示器显示画面每秒刷新的次数,现在的电脑显示屏刷新率一般为75Hz,如果刷新率在...

小马激活工具网址(小马激活工具是什么东西)
  • 小马激活工具网址(小马激活工具是什么东西)
  • 小马激活工具网址(小马激活工具是什么东西)
  • 小马激活工具网址(小马激活工具是什么东西)
  • 小马激活工具网址(小马激活工具是什么东西)
windows7激活工具 知乎(win7激活工具怎么使用教程)

Win7激活工具有很多,比如kms激活工具、小马激活工具、Windowsloader等。下面以这三款激活工具为例,做一个简单的比较。1、kms激活工具,相对比较稳定,通用性强,对各种gho、iso镜...

英伟达高端显卡排行(英伟达最高级显卡)

具体的排名如下:1、NVIDIAGeForceRTX30902、NVIDIAGeForceRTX3080Ti3、NVIDIAQuadroRTXA60004、NVIDIAGeFor...

苹果电脑为啥不能玩游戏(买苹果电脑的十大忠告)

1、MacBook本身就不是用来玩游戏的,是用来轻度办公的,只有集成显卡没有独立显卡,玩游戏也会非常卡。2、MacOS系统虽然支持steam软件,但是里面的游戏并不支持MacOS,况且本身支持MacO...

笔记本电脑显卡驱动怎么安装

使用驱动精灵或者驱动之家搜索驱动,下载后会自动安装驱动的。您好,以下是安装MacBook独立显卡驱动的步骤:1.打开“应用程序”,找到“实用工具”文件夹,打开“终端”。2.在终端窗口中输入以下命令...

家庭wifi已连接不可上网(家里wifi已连接不可用是什么原因)

WiFi已连接不可上网原因和解决方法一、路由器不稳定有些无线路由器、光猫(宽带猫)的质量比较差,长时间运行后会出现死机等一系列的问题。解决办法:把你家里的无线路由器、光猫(宽带猫)断电,等待几分钟,然...

win10专业版在哪下(win10专业版在哪下载)

1.登陆MicroSoft官方网站会员中心,https://insider.windows.com/?lc=1033,点击“登陆”。2.使用hotmail邮箱或者开发者帐号登陆即可。3.选择“获...