STL-简单学习总结

本文详细介绍了C++中一维、二维和三维向量(vector)的使用,以及队列(queue),优先队列(priority_queue),集合(set),映射(map)和pair的数据结构,展示了它们的基本操作和遍历方法。
摘要由CSDN通过智能技术生成

直接放源代码

#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>

using namespace std;


int main()
{
	//定义一维的容器;
	vector<int> vec(100, 1);//括号里面是(长度,构造初始化的值);
	//定义二维的容器;
	vector<vector<int>> vec_1(5, vector<int>(6, 10));//5行6列的vector ;
	//定义三维的容器;
	vector<vector<vector<int>>>vec_2(5, vector<vector<int>>(6, vector<int>(4, 10)));//等价于int vec[5][6][4];
/*---------------------------------------------------------------------------------------------------------------------------------*/
	//下面是有关于vector的一些函数;
	/*1:push_back()*/
	vector<int> arr;
	arr.push_back(1);
	arr.push_back(2);
	arr.push_back(3);
	arr.push_back(4);
	arr.push_back(5);
	arr.push_back(6);
	for (auto& ele : arr)
		cout << ele << endl;
	/*2.pop_back()*/
	arr.pop_back();
	arr.pop_back();
	arr.pop_back();
	cout << "pop_back()" << endl;
	for (auto& ele : arr)
		cout << ele << endl;
	/*3.size*/
	cout << "size();" << endl;
	cout << arr.size() << endl;

	/*clear()*/
	arr.clear();
	cout << arr.size() << endl;

	/*empty*/
	if (arr.empty())cout << "gg" << endl;

	/*resize()*/
	arr.resize(5);
	cout << "---------------------------------------------------" << endl;
	for (auto& t : arr)
		cout << t << " "; 

	/*-----------------------------------------------------------------------------------------------------------------------*/

	//队列;

	queue<int> que;

	que.push(1);
	que.push(2);
	que.push(3);

	cout << que.front() << endl;// 队首;
	cout << que.back() << endl;//队尾;

	que.pop();

	cout << que.front() << endl;// 队首;
	cout << que.back() << endl;//队尾;
	cout << que.size() << endl;
	cout << que.empty() << endl;

	cout << "-------------------------------------------------------------------------------------------------------" << endl;
	//优先队列
	 
	priority_queue<int>pque;//大顶堆;
	pque.push(1);
	pque.push(3);
	pque.push(5);
	pque.push(2);
	cout << pque.top() << endl;

	pque.pop();//把最大元素弹掉;
	pque.pop();
	pque.pop();
	cout << pque.top() << endl;


	priority_queue<int, vector<int>, greater<int>>pque1;//priority_queue<int, 底层容器, 仿函数>pque1;//小顶堆;


	cout << "---------------------------------------------------------------------------------------------------------" << endl;

	//集合set;

	set<int> st;
	st.insert(1);
	st.insert(2);
	st.insert(2);
	st.insert(1);
	st.insert(0);
	for (auto& ele : st)
		cout << ele << endl;

	st.erase(2);//删除集合当中的一个元素;
	for (auto& ele : st)
		cout << ele << endl;

	if (st.find(1) != st.end())//st.find()返回指向1的迭代器,若没有1则返回尾迭代器st.end();
	{
		cout << "yes" << endl;
	}

	for (auto& ele : st)
		cout << ele << endl;

	
	cout << st.count(1) << endl;//返回制定元素有几个,在这里只可能返回0or1,因为set集合元素的互异性,n个1还是为1个1;

	cout << st.size() << endl;
	st.clear();
	cout << st.empty() << endl;

	/*遍历*/
	/*1.用迭代器进行遍历*/
	for (set<int>::iterator it = st.begin(); it != st.end(); ++it)
		cout << *it << endl;
	/*c++语法遍历*/
	for (auto& ele : st)
		cout << ele << endl;



	cout << "----------------------------------------------映射-------------------------------------------------------------------" << endl;
	/*map*/
	/*map 可以提供任意类型到任意类型的映射*/

	map<string, int>a;
	a["hzr"] = 1;
	a["hzh"] = 2;
	a["mse"] = 3;
	a["jbl"] = 4;
	/*一个键(“hzr”..)只能出现一次不可以a["hzr"] = 1;a["hzr"] = 3;*/
	/*键是没有顺序的*/

	map<int, int>mp;//第一个是键的参数,第二个是值的参数,键--》值;

	mp[2] = 1;
	mp[2] = 2;

	mp.erase(2);

	cout << mp.count(1) << endl;//只能返回0or1;

	if (mp.find(2) != mp.end())puts("yes");
	else puts("NO");

	cout << mp.size() << endl;
	mp.clear();
	cout << mp.empty() << endl;


	/*遍历整个map*/
	for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it)
		cout << it->first<<' '<<it->second;/*map里面的元素是用pair 来存的*/
	/*或者用c++语法遍历*/
	for (auto& pr : mp)
		cout << pr.first <<' '<< pr.second << endl;
	
	cout << "--------------------------------------------string------------------------------------------------------------------"<<endl;
	/*子串的查找*/
	string s1 = "123123123";
	cout << s1.find("321") << endl;//找不到会返回npos的树;
	if (s1.find("123") != string::npos)puts("Yes");
	else puts("No");

	/*数字与字符串的转换*/
	/*1.*/
	int x = 6665;
	string s = to_string(x);
	cout << s << endl;
	/*2.*/
	string s3 = "1346.5";
	int x2 = stoi(s3);
	float x3 = stod(s3);
	cout << x2 <<' '<<x3 << endl;


	cout << "-------------------------------------------------pair---------------------------------------------------------------------" << endl;
	/*二元组*/
	pair<int, int>p = { 1,2 };
	cout << p.first << " " << p.second << endl;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值