饥饿学习STL有感

今天饥饿学习了STL的部分标准模板库,好久没写博客了,想着写一发!!
1.unordered_map , 他是按照插入顺序输出的,但是具体还是得看编译器源代码中的Hash表,hhh

#include<unordered_map>
using namespace std;
unordered_map<int , string > mymap ;
string s = "赵浩飞";
cout << s.length() << endl;
mymap.insert(make_pair(6 , "羽毛球")); //插入数据方式1 
mymap.insert(make_pair(5 , "zhf"));
mymap.insert(make_pair(4 , "ljh"));
mymap.insert(make_pair(3 , "love"));
mymap.insert(pair<int ,string>(9 , "dd"));  //插入方式2 
auto iter = mymap.begin();
while(iter != mymap.end()){
	cout << iter->first << "," << iter->second << endl;
	iter ++;
} 
auto iterator = mymap.find(3);
if(iterator != mymap.end()) cout << endl << iterator->first << iterator->second ;

2.map , 因为内部红黑树 , 他是按照first的顺序排列的。char数组是不能作为键值的,只能用string。
map中的键是唯一的。

#include<map>
using namespace std;
map<int , string> mymap;
mymap.insert(pair<int , string>(1 , "飞哥")); //插入方式1 
mymap.insert(make_pair(3 , "弟弟")); 		 //插入方式2 
mymap.insert(make_pair(0 , "ddd"));
cout << mymap.size() << endl;
map<int , string > :: iterator iter;
for(iter = mymap.begin() ; iter != mymap.end() ; iter ++){
cout << iter -> first << " " << iter -> second << endl ;
	
iter = mymap.find(1);   //返回键为 1 的映射的迭代器 
cout << iter -> first << " " << iter -> second << endl ;
	
mymap.erase(iter); //删除单个元素,  mymap.erase(first , last) 删除[first , last) 区间内的所有元素 
cout << mymap.size() << endl;
	
mymap.clear();   //清空map
cout << mymap.size() << endl;

3.stack 栈

#include<stack>
using namespace std;
stack<int> myst;
for(int i = 0 ; i < 5 ; i ++){
	myst.push(i);    //插入数据 
}
cout << myst.size() << endl;  //栈元素个数 
if(myst.empty() == false)
	cout << myst.top() << endl; //获得栈顶元素 
myst.pop();   //弹出栈顶元素
cout << myst.size() << endl; 
if(myst.empty() == false)
	cout << myst.top() << endl;
if(myst.empty() == true) cout << "空" << endl;   //判断栈空与否 
else cout << "非空" << endl;

4.priority_queue 优先队列

#include<queue>
using namespace std;
priority_queue<int> p; //等价于 priority_queue<int , vector<int> , less<int> > 
//数字越小优先级越大 , 则: priority_queue<int , vector<int> , greater<int> > 
for(int i = 0 ; i < 5 ; i ++) p.push(i);  //插入数据 , 先插入的是1 但是先输出的是4 , 因为默认越大优先级越高 
cout << p.size() << endl ;
if(p.empty() == false)
	cout << p.top() << endl ;   //输出栈顶元素 ,优先对列没有front() 和 back()函数,只能通过top()访问队首元素 
p.pop();   //弹出栈顶元素
cout << p.size() << endl ;
if(p.empty() == false)
	cout << p.top() << endl ;

今天就说这么多吧hhh,实在是饿啦!

posted @ 2020-09-20 17:51  时光以北  阅读(120)  评论(0编辑  收藏  举报