优秀的编程知识分享平台

网站首页 > 技术文章 正文

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

nanyue 2025-02-21 14:36:12 技术文章 3 ℃


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

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

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

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

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


示例代码1 string基础操作

#include
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删除以后的结果" <

示例代码2 stack基础操作

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

	stack 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 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 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



*/
#include
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::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::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::iterator it1 = l.begin();
	list::iterator it2 = l.begin();
	it2++;
	it2++;
	it2++;
	l.erase(it1, it2);
	for (list::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
using namespace std;
#include"deque"
#include"algorithm"
void printD(deque&d)
{
	for (deque::iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << endl;
	}
}
void main11()
{
	deque 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 << "尾部元素" <::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
using namespace std;
#include
#include"list"
#include"set"
void main11()
{

	multiset setl;
	int tmp = 0;

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


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

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

最近发表
标签列表