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

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

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


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;
}

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

相关推荐

C/C++恶意代码盘点(一):进程遍历丨木马病毒丨密码记录

恶意代码的分类包括计算机病毒、蠕虫、木马、后门、Rootkit、流氓软件、间谍软件、广告软件、僵尸(bot)、Exploit等等,有些技术经常用到,有的也是必然用到。恶意代码常见功能技术如下:进程遍...

跨越十年的C++演进:C++11新特性全解析

原作者:Linux教程,原文「链接」:https://mp.weixin.qq.com/s/oFbiFlqiwgVcJIMMvTelEA很多刚刚进入C++领域的朋友,最初是从C语言转过来的。因...

如何在C#中调用C++方法(c#调用c++的类)

主要方式C#主要通过两种方式提供对非托管代码的调用,第一种是使用平台调用(PlatformInvoke,P/Invoke),第二种是使用不安全代码(unsafe),日常开发中我们使用最多的就是第一种...

C语言字符数组和字符串(c语言字符数组和字符串数组)

用来存放字符的数组称为字符数组,例如:charc[10];字符数组也可以是二维或多维数组。例如:charc[5][10];字符数组也允许在定义时进行初始化,例如:charc[10]={'c',...

C语言指针,如何操作字符串?linux C第45讲

1指针操作字符串在学习数组的时候,我们了解了字符串的定义,我们可以定义一个字符数组,用来存放一个字符串,例如:chararray[]={"abcde"};charbuf[]=...

信奥赛C++常用的算法总结(信息学奥赛c语言和c++有什么区别)

1、桶排序核心:①创建盛下所有数的数组②将每个数作为编号放入桶里优点:稳定、简单、容易考缺点:空间复杂度较大,时间复杂度较大。#include<bits/stdc++.h>usingn...

Arduino 使用 C 字符串(arduino 字符串 数组)

问题您想了解如何使用原始字符字符串:创建字符串、查找其长度以及比较、复制或附加字符串。核心C语言不支持Arduino样式的String功能,因此您想了解针对基本字符数组编写的其他平台的代码...

C++20 四大特性之一:Module 特性详解

C++20最大的特性是什么?最大的特性是迄今为止没有哪一款编译器完全实现了所有特性。文章来源:网易云信有人认为C++20是C++11以来最大的一次改动,甚至比C++11还要大。本文仅介绍...

C/C++的const常量总结(c++语言const)

“所谓常量,就是在代码运行过程中值恒定不变的标识符,该标识符的值可以是一个常数,也可以是字符串。”在C/C++中,通常使用define宏定义或者const来定义常量,比如:#definePI3....

学习分享 | 通过C++python的对比,帮你快速入门python

一、前言对于只接触过静态语言,而从未使用过动态语言的人来说,第一次看到python的语法可能会大为惊叹。不用申明变量类型?不用等老半天编译完成就能直接运行?不用小心的维护指针?还不用写CMakelis...

字符串常量,C语言字符串常量详解

字符常量是由一对单撇号括起来的单个字符,如'a'、'D'、'?'、'#39;。在C语言中,除了字符常量外还有字符串常量,顾名思义就是多个“...

C/C++中的内存四区(c++的内存区域分为)

1代码区存放CPU执行的机器指令。通常代码区是可共享的(即另外的执行程序可以调用它),使其可共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可。代码区通常是只读的,使其只读的原因是防...

通过pybind11来实现python调用C++接口(一)

有小伙伴很好奇,怎么样实现python调用C++接口?哈哈,手把手教程来了。第一步:我们需要安装pybind11这个纯头文件的库,目前该库支持c++11及以上版本,在你的环境中通过命令行输入:apt-...

深入了解C++如何注释以及在哪儿注释-开课吧广场

注释虽然写起来很痛苦,但对保证代码可读性至关重要,同时这也是每一个C++开发工程师所需要做好的事情。那么C++开发过程中该如何注释?应该在哪写注释呢?关于注释风格,很多C++的Coders更喜欢行注释...

C++核心知识点速查手册(实用重点版)

一、基础必备核心1.指针与引用(遥控器原理)指针:存储地址的变量(像电视遥控器)inttv=100;//电视机int*remote=&tv;//遥控器指向电视*...