C++学习 2018-12-18

C++学习:STL容器与操作
这篇博客介绍了C++中的STL容器,包括vector、stack、queue和map的使用。通过示例展示了如何在vector中装入list,解释了栈和队列的操作规则,以及map的创建、遍历和查找功能。最后,应用map解决公交车站线路查询问题。

1.在vector中装入一个list

1.第一个数组装1,第二个装1、2,第三个装1、2、3,以此类推,第七个装1、2、3、4、5、6、7;使用迭代器进行输出。
int main()
{
	vector<list<int>> vec(7);
	for(int i=0; i<7; i++)
	{
		for(int j=0; j<=i; j++)
		{
			vec[i].push_back(j+1);
		}
	}
	// vector<int>::iterator ite = vec.begin();
	for(int i=0; i<7; i++)
	{
		list<int>::iterator ite = vec[i].begin();
		while(ite != vec[i].end())
		{
			cout << *ite << endl;
			++ite;
		}
	}

	system("pause");
	return 0;
}

2.stack栈和queue队列

1.栈和队列都是在固定的一端进行操作,并且初始和终止状态都应为空,都是临时存储数据的(vector和list是保存数据的)。
2.进栈的顺序:1234,则不可能的出栈顺序有:3124
3.对stack sk进行push和pop操作以及使用top查看栈顶元素。
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
	stack<int> stack1;
	stack1.push(11);
	stack1.push(12);
	stack1.push(13);
	stack1.push(14);
	stack1.push(15);
	stack1.push(16);
	while (!stack1.empty())
	{
		cout << stack1.top() << endl;
		stack1.pop();
	}

	system("pause");
	return 0;
}
4.对queue qu进行push操作以及查看其中元素。
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
	queue<int> qu;
	qu.push(20);
	qu.push(21);
	qu.push(22);
	qu.push(23);
	qu.push(24);
	qu.push(25);
	qu.push(26);
	while (!qu.empty())
	{
		cout << qu.front() << endl;
		qu.pop();
	}

	system("pause");
	return 0;
}

3.容器分为序列容器和关联容器(有映射关系)两类

4.map映射表

1.包含key与value(类似于python中的dict)
2.创建map
int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	system("pause");
	return 0;
}
3.遍历map

1.定义迭代器

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	map<string, int>::iterator ite = mp.begin();
	while(ite != mp.end())
	{
		cout << ite->first << " " << ite->second << endl;
		++ite;
	}

	system("pause");
	return 0;
}

2.algorithm

void ShowMap(pair<string, int> pr)
{
	cout << pr.first << " " << pr.second << endl;
}

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	::for_each(mp.begin(), mp.end(), &ShowMap);

	system("pause");
	return 0;
}
4.查找某元素
void ShowMap(pair<string, int> pr)
{
	cout << pr.first << " " << pr.second << endl;
}

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	map<string, int>::iterator ite = mp.find('B');
	mp.erase(ite);
	::for_each(mp.begin(), mp.end(), &ShowMap);

	system("pause");
	return 0;
}
5.lower_bound成员函数

查找某key值对应的value值是否存在,若存在则返回pair,不存在返回与查找的key接近的下一个pair。

void ShowMap(pair<string, int> pr)
{
	cout << pr.first << " " << pr.second << endl;
}

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	map<string, int>::iterator ite1 = mp.lower_bound('A');
	cout << ite1->first << " " << ite1->second << endl;
	map<string, int>::iterator ite2 = mp.lower_bound('C');
	cout << ite2->first << " " << ite2->second << endl;

	system("pause");
	return 0;
}
6.upper_bound成员函数

返回查找的下一个。

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	map<string, int>::iterator ite1 = mp.upper_bound('A');
	cout << ite1->first << " " << ite1->second << endl;
	map<string, int>::iterator ite2 = mp.upper_bound('C');
	cout << ite2->first << " " << ite2->second << endl;

	system("pause");
	return 0;
}
7.count成员函数

查找key值出现的次数,因为在map中不允许有同名的key值,因此该函数可以当作检测key是否存在,若存在则返回1,否则返回0。

int main()
{
	map<string, int> mp;
	mp['A'] = 100;
	mp['S'] = 300;
	mp['Q'] = 400;
	mp['E'] = 700;
	mp['G'] = 800;
	mp['H'] = 200;
	mp['B'] = 500;
	mp['V'] = 900;

	cout << mp.count('A') << endl;
	cout << mp.count('C') << endl;

	system("pause");
	return 0;
}

5.公交车站

使用map完成三个站点的站名、所经过的线路,并键盘输入起始站和终点站,找出可由起始站到达终点站的公交线路。

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

int main()
{
	map<string, list<int>> bus_station;
	bus_station["理工大学"].push_back(11);
	bus_station["理工大学"].push_back(104);
	bus_station["理工大学"].push_back(107);
	bus_station["理工大学"].push_back(68);
	bus_station["理工大学"].push_back(203);

	bus_station["工程大学"].push_back(203);
	bus_station["工程大学"].push_back(105);
	bus_station["工程大学"].push_back(16);
	bus_station["工程大学"].push_back(49);
	bus_station["工程大学"].push_back(57);
	
	string begin_station, end_station;
	cout << "请输入起点站:";
	cin >> begin_station;
	cout << "请输入终点站:";
	cin >> end_station;

	list<int>::iterator ite_begin = bus_station[begin_station].begin();
	while(ite_begin != bus_station[begin_station].end())
	{
		list<int>::iterator ite_end = bus_station[end_station].begin();
		while(ite_end != bus_station[end_station].end())
		{
			if(*ite_begin == *ite_end)
			{
				cout << *ite_begin << "能到达" << endl;
			}
			++ite_end;
		}
		++ite_begin;
	}

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值