C++ 常用的STL库

C语言: 数据结构有很多种-数组-链表-队列-栈-树 需要自己写代码实现
C++ 提供了STL库,里面已经完成了所有数据结构,内部全部都是使用模板实现的.

 C++提供了数据结构的 模板库,给你用.
 STL库包括: 数组  链表 队列 栈 数  图 .......
      这些结构又称为##容器##
      STL库中的容器 空间都是动态扩展的.

几种常见的容器:
vector --数组
1.随机访问 2.删除/插入很麻烦

#include<iostream>
using namespace std;
#include<vector>

vector<string> k;
int main()
{
	k.push_back("黄");
	k.push_back("胡");	
	k.push_back("王");
	k.push_back("徐");	
	k.push_back("景");
	k.push_back("龙");
	
	k.pop_back();
	
	cout<<k.at(0)<<k.at(1)<<k.at(2)<<k.at(3)<<k.at(4)<<endl;
	/*for(int i=0;i<k.size();i++)
	{
		cout<<i<<k[i]<<endl;
	}*/
	//迭代器
	vector<string>::iterator it;
	cout<<"data....."<<endl;
	for(it=k.begin();it!=k.end();it++)
	{
		cout<<"	"<<*it;
	}
	cout<<endl;
	
	//插入
	it=k.begin();for(int i=0;i<4;i++){it++;}
	k.insert(it,"陆");
	cout<<"insert...."<<endl;
	for(vector<string>::iterator  it=k.begin();it!=k.end();it++)
	{cout<<"	"<<*it;}
	cout<<endl;
	//删除
	it=k.begin();for(int i=0;i<4;i++){it++;}
	k.erase(it);
	cout<<"erase...."<<endl;
	for(vector<string>::iterator  it=k.begin();it!=k.end();it++)
	{cout<<"	"<<*it;}
	cout<<endl;
	
	cout<<"k is empty"<<k.empty()<<endl;
	k.clear();//清空
	cout<<"k is empty"<<k.empty()<<endl;//判空
	return 0;
}

更高级的遍历方式: 迭代器----本质上其实就是指针 it++下一个元素 it–上一个元素 *it求内容
1.定义迭代器 (指针)
vector::iterator it; it就是一个迭代器(指针)
2.如何获取对象的 头迭代器(头指针), 尾迭代器(指向尾部的下一个) it = v.begin(); it=v.end();

list -链表
1.不能随机访问 2.插入 删除很轻松

#include<iostream>
using namespace std;

#include <list>

list<int> ll;

int main()
{
	/*
		尾部追加
		void push_back(T val);
		
		尾部删除
		void pop_back()
		
		查看尾部元素			取出尾部元素: 先查看,再删除
		T back();
	*/
	ll.push_back(11); ll.push_back(22); ll.push_back(33); ll.push_back(44);
	//提出尾部元素
	cout<<"back is "<<ll.back()<<endl;   ll.pop_back();
	
	/*
		头部追加 
		void push_front(T val)
		头部删除 
		void pop_front();
		查看头部  T front();
	*/
	ll.push_front(9); ll.push_front(8);ll.push_front(7);
	//提取头部元素 
	cout<<"front is "<<ll.front()<<endl; ll.pop_front();
	
	/* 迭代器遍历    
	*/
	cout<<"travel first :";
	for(list<int>::iterator it = ll.begin();  it!=ll.end();  it++){
		cout<<" "<<*it;
	}
	cout<<endl;
	
	/*
		插入 insert (it, T val)
	
	*/
	list<int>::iterator it=ll.begin(); for(int i=0;i<4;i++){it++;}
	ll.insert(it,10);
	
	cout<<"travel insert ... :";
	for(list<int>::iterator it = ll.begin();  it!=ll.end();  it++){
		cout<<" "<<*it;
	}
	cout<<endl; 	
	
	/*
		删除 erase(it)  
	*/
	it=ll.begin(); it++;it++;
	ll.erase(it);
	cout<<"travel erase ... :";
	for(list<int>::iterator it = ll.begin();  it!=ll.end();  it++){
		cout<<" "<<*it;
	}
	cout<<endl;		
	
	/*
		int size(); 返回元素个数
		bool empty(); 判空
		void clear();
	*/
	cout<<"cnt="<<ll.size()<<endl;
	cout<<"list empty? "<<ll.empty()<<endl;
	ll.clear();
	cout<<"list empty? "<<ll.empty()<<endl;
	
	
}

dqueue --队列

1.尾部插入 头部删除 2.求个数 3.判空 4. 遍历

#include<iostream>
using namespace std;

#include <deque>

deque<int>  q; 

int main()
{
	/*
		尾部插入
		void push_back(T val)
	*/
	q.push_back(11);q.push_back(22);q.push_back(33);q.push_back(55);q.push_back(55);
	/*
		头部删除 
		void pop_front(void)
		T front()
		
		判空 bool empty();
	*/
	while( q.empty() != true  ){
		cout<<" "<< q.front() ;
		q.pop_front();
	}
	cout<<endl;
	
	
	q.push_back(11);q.push_back(22);q.push_back(33);q.push_back(55);q.push_back(55);
	for(deque<int>::iterator it = q.begin();  it!=q.end();it++   ){
		cout<<" "<<*it;
	}
	cout<<endl;
	
	/*
		int size();获取个数
	*/
	cout<<" q cnt="<<q.size()<<endl;
	
	/*
		clear() 清空
		
	*/
	cout<<"q empyt? "<<q.empty()<<endl;
	q.clear();
	cout<<"q empyt? "<<q.empty()<<endl;

}

stack --栈

1.入栈 与出栈 2.求个数

#include<iostream>
using namespace std;
#include <stack>
stack<string> sk;

int main()
{
	/*入栈 push(T val)  
	
		出栈 void pop(void)
		查看顶部值: T top()  ,只是简单的看而已
		
		判空:
			bool empty();
			
		个数
			int size()
	*/
	sk.push("red"); sk.push("blue"); sk.push("yellow");sk.push("greed");
	cout<<"cnt="<<sk.size()<<endl;
	
	while(sk.empty() != true ){
		cout<<sk.top()<<"  ";
		sk.pop();
	}
	
	cout<<"stack empty "<<sk.empty()<<endl;
	
	sk.push("red"); sk.push("blue"); sk.push("yellow");sk.push("greed");
	cout<<"stack empty "<<sk.empty()<<endl;
	return 0;
	
}

​ map --树

#include<iostream>
using namespace std;
#include<map>
int main()
{
	map<string,int> Map;//定一个以string为键值的map数组 
	
	Map["a"]=1;//把1赋值给下标为"a" 
	
	Map.insert(map<string,int>::value_type("b",2));
	Map.insert(pair<string,int>("c",114));
	Map.insert(make_pair<string,int>("d",4));
	//向map插入一个元素,第一个为键值,第二个为值
	
	cout<<Map["a"]<<' '<<Map["b"]<<' '<<Map["c"]<<' '<<Map["d"]<<'\n';
	Map.insert(make_pair("b",3));
	//map的insert只能插入一个,重复插入为无效操作 
	
	Map["a"]=2;
	//赋值与插入不同,可以重复替换 
	
	cout<<Map["a"]<<' '<<Map["b"]<<' '<<Map["c"]<<' '<<Map["d"]<<'\n';
	int x=Map["c"];
	cout<<x<<'\n';
	int y=Map["ccccc"];
	cout<<x<<'\n';
	//当它不能够找到这个值时,则自动插入一个,值为初始化
	
	map<string,int>::iterator it; 
	int key;
	it=Map.find("c");
	if (it!=Map.end())//在map中如果找不到返回map.end()
		key=it->second;//表示key等于这个键值 
		
	cout<<key<<' '<<Map["c"]<<'\n';
	
	bool i_t;
	i_t=Map.count("c");
	//如果"c"在Map中返回1,否则返回0 
	
	if (i_t) cout<<"yes\n";
	else cout<<"no\n";
	for(it=Map.begin();it!=Map.end();it++)
		cout<<it->first<<' '<<it->second<<'\n';
	//使用了迭代器i_t分别指向map的键值和实际值
	// first为键值, second为实际值 
	
	cout<<'\n';
	for(const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
	//当把迭代器这样用时,它是一个具体的值,即一个pair类型 
	
	cout<<'\n';
	map<int,string> Map2;
	Map2[1]="a";
	Map2[2]="b";
	Map2[3]="c";
	for(int i=1;i<=3;i++)
		cout<<Map2[i]<<' ';
	cout<<'\n';
	//只用但我们的键值时可以枚举的时候我们才可以这样遍历
	//例如string类型就不可以
	
	map<string,int>::iterator i__t;
	i__t=Map.find("a");
	Map.erase(i__t);
	//使用迭代器删除,先找到为键值为"a"的值,并且把它删除
	 
	for (const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
		
	int n=Map.erase("b");
	if (n)cout<<"yes\n";
	else cout<<"no\n";
	
	for (const auto& tmp:Map)
		cout<<tmp.first<<' '<<tmp.second<<'\n';
	
//	forward_list 单向链表,基本没啥用 
//	unordered_+容器 ,就是用hash来存贮 
	map<string,int>::iterator i,j;
	i=Map.begin();
	j=Map.end();
	i++;
	j++;
	Map.erase(i,j); //删去i,j之间的区间 
	return 0;
}

​ 图

NULL…

未学习 日后补充

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小镇春风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值