C++学习-STL容器

STL中的vector:

/*
	vector --->向量  ---->线性容器
	用标准模板,记得加上相应的头文件
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
	//向量容器
	vector <int> v0;
	//初始化值
	vector <int> v1{ 1,2,3,4,5,9,6 }; //设置向量容量
	//初始化
	v1[0] = 8;
	v1[1] = 8;
	v1[2] = 8;
	//声明迭代器 标准他属于那个模板
	vector<int>::iterator  v1_Iter;
	for (v1_Iter = v1.begin(); v1_Iter < v1.end(); v1_Iter++)
	{
		cout <<" "<< *v1_Iter;
	}
	cout << endl;
	//逆序迭代器
	vector<int>::reverse_iterator  v1_rIter;
	for (v1_rIter = v1.rbegin(); v1_rIter < v1.rend(); v1_rIter++)
	{
		cout << " " << *v1_rIter;
	}

	//新标准
	cout << endl;

	for (int &i : v1)
	{
		cout << " " << i;
	}
	system("pause");
	return 0;
}

STL中的stack:

#include <iostream>
#include<stack>
#include <string>
using namespace std;
int main()
{
	//stack 先进后出
	stack<string> mystack;
	mystack.push("饭");
	mystack.push("要吃");
	mystack.push("我"); 
	cout << mystack.size();
	cout << endl;
	while (!mystack.empty())
	{
		cout << mystack.top();
		mystack.pop();
	}
	cout << endl;

	system("pause");

	return 0;
}

STL中的queue:

#include <iostream>
#include<queue>
#include <list>
#include <string>
using namespace std;
int main()
{
	queue<string> qs;

	//嵌套
	list<string> Ls;
	Ls.push_back("12123");
	queue<list<string>> QS;
	QS.push(Ls);
 
	qs.push("我");
	qs.push("想");
	qs.push("你");
	while (!qs.empty())
	{
		cout << qs.front();
		qs.pop();
	}
	cout << endl;
	system("pause");
	return 0;
}

STL中的list:

#include<iostream>
#include <list>	//俗称  列表
using namespace std;
int main()
{
	list<int>  L1;

	L1.push_back(3);
	L1.push_back(2);
	L1.push_back(1);
	for (int &v : L1)
	{
		cout << " " << v;
	}
	cout << endl;
	L1.push_front(4);

	list<int>::iterator  L1_Iter;
	//-----> p->next!= NULL
	for (L1_Iter = L1.begin(); L1_Iter!=L1.end(); L1_Iter++)
	{
		cout << " " << *L1_Iter;
	}
	system("pause"); //防止闪屏
	return 0;
}

STL中的set:

#include <iostream>
#include<set>
#include <string>
using namespace std;
int main()
{
	/*set <int> si;*/
	multiset <int> si;
	//没有重复出现的

	si.insert(1);
	si.insert(2);
	si.insert(2);
	si.insert(4);
	for (set<int>::const_iterator it = si.begin(); it != si.end(); it++)
	{
		cout << " " << *it;
	}
	cout << endl;

	set<string> bad_ip;
	bad_ip.insert("192.168.1.1");
	bad_ip.insert("192.168.1.10");
	string my_ip = "192.168.1.1";
	//count使用
	if (bad_ip.count(my_ip) != 0)
	{
		cout << "该ip属于黑名单" << endl;
	}

	system("pause");
	return 0;
}

STL中的map:

#include<iostream>
#include <string>
#include <map>
using namespace std;
int main()
{

	//初始化方式
	//----->字典
	//first:键   second:值 

	//map<string,int> msi
	//pair:数对类型 
	//结构体+类,显式实例化
	//msi.insert(pair<string, int>("张飞", 97));
	make_pair() 函数隐式
	//msi.insert(make_pair("赵云", 78));
	//下标的方式进行访问
	//msi["a"] = 100;
	//msi["A"] = 300;
	//msi["f"] = 40;
	//msi["d"] = 500;
	//msi["e"] = 1000;

	//map :1.注意:键唯一
	//	   2.有序,排序 大写大于小写,其他按字母  "A">"a"  
	//typedef map<string, int>::iterator IT;
	//for (IT it = msi.begin(); it != msi.end(); ++it)
	//{
	//	cout << "名字:" << it->first << "\t战力:" << it->second<<endl;
	//}
	multimap<string, int> msi;
	msi.insert(make_pair("ZF", 95));
	msi.insert(make_pair("ZL", 95));
	typedef multimap<string, int>::iterator IT;
	for (IT it = msi.begin(); it != msi.end(); ++it)
	{
		cout << "名字:" << it->first << "\t战力:" << it->second<<endl;
	}
	system("pause");
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值