饥饿学习STL有感

时间:2020-09-20
本文章向大家介绍饥饿学习STL有感,主要包括饥饿学习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的顺序排列的。
#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")); map<int , string > :: iterator iter; for(iter = mymap.begin() ; iter != mymap.end() ; iter ++){ cout << iter -> first << " " << iter -> second << 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,实在是饿啦!

原文地址:https://www.cnblogs.com/ZhaoHaoFei/p/13701159.html