目录
五、queue的常见用法
queue也就是队列,是STL中实现先进先出的容器。queue是在当需要实现BFS的时候,可以不用自己去手动实现一个队列,而是用queue作为代替,提高代码的准确性。另外要注意的一点是,在使用front()和back()函数之前,需要判断queue是否为空。
- queue的定义:
/* queue的定义在STL中比较常规:添加头文件#include <queue> 并且在头文件下边加上using namespace std;然后就可以使用了。 使用模式是queue<typename> name; */ queue<int> q;
- queue的访问:
/* STL中只能通过front()来访问队首元素,或者通过back()函数来访问队尾元素 */ printf("%d %d\n",q.front(),q.back());
- push()函数:
/* push(x)将x进行入队 */ q.push(x);
- front()和back():
/* front()函数获得队首元素,back()函数获得队尾元素 */ printf("%d %d\n",q.front(),q.back());
- pop()函数:
/* pop()函数用于让队首元素出队 */ printf("%d\n",q.pop());
- empty()函数:
/* empty()函数检查队列是否为空,返回bool类型,true是空,false是非空 */ if(!q.empty()){ ……; }
六、priority_queue的常见用法
priority_queue又称为优先队列,其底层是利用堆实现的。在priority_queue中,队首元素一定是优先级最高的元素,任何时间向priority_queue中push元素都会随时调整结构,使得每次的队首元素都是优先级最大的。priority_queue可以解决一些贪心问题,也可以对Dijkstra算法进行优化。
- priority_queue的定义:
/* priority_queue的定义在STL中比较常规:添加头文件#include <queue> 并且在头文件下边加上using namespace std;然后就可以使用了。 使用模式是priority_queue<typename> name; */ priority_queue<int> q;
- priority_queue容器内元素的访问:
/* priority_queue没有front()函数和back()函数, 只能通过top()函数来访问队首元素,也就是优先级最高的元素 */ printf("%d",q.top());
- push()函数:
/* push(x)将x进行入队 */ q.push(x);
- top()函数:
/* top()函数获得队首元素 */ printf("%d",q.top());
- pop()函数:
/* pop()函数就是令队首元素出队的元素,就是优先级最高的元素 */ q.pop();
- empty()函数:
/* empty()函数检测priority_queue是否为空, 空返回true,非空返回false; */ if(q.empty()==true) if(!q.empty())
priority_queue内元素优先级的设置:
- 基本数据类型 的优先级设置
/* 以下两则等价,less<int>表示数字大的优先级大,而greater<int>表示数字小的优先级大 */ priority_queue<int> q; priority_queue<int,vector<int>,less<int> > q; /*----------------------------------------------------------*/ priority_queue<int,vector<int>,greater<int> > q;
-
结构体优先级设置
/*举个栗子帮助理解*/ /* 价格大的优先级大,与sort函数定义相反 */ struct fruit{ string name; int price; friend bool operator < (fruit f1,fruit f2){ return f1.price < f2.price; //return f1.price > f2.price; 价格大的优先级小 } } /*-------------------------------------------------------------*/ /*除了上面一种办法,还有一种办法表示*/ struct fruit{ string name; int price; } struct cmp{ bool operator ()(fruit f1,fruit f2){ return f1.price < f2.price; //return f1.price > f2.price; 价格大的优先级小 } /*引用*/ priority_queue<fruit, vector<int>,cmp> q; /*即使是基本数据类型或者其他STL容器(如set),也可以通过同样的方式来定义优先级*/ friend bool operator < (const fruit &f1,const fruit &f2){ return f1.price >f2.price; } bool operator () (const fruit &f1,const fruit &f2){ return f1.price > f2.price; }
七、stack的常见用法
栈,后进先出(FILO)
- stack的定义:
/* 定义一个stack需要添加头文件#include <stack> 以及在头文件下方加上using namespace std; stack<int> st; */ stack<int> st;
- stack容器内的元素访问:
/* 只能通过top()函数来访问栈顶元素 */ st.top();
- push()函数:
/* push(x)将x入栈 */ st.push(1);
- top()函数:
/* top()函数获得栈顶元素 */ st.top();
- pop()函数:
/* 使用pop()函数弹出栈顶元素 */ st.pop();
- empty()函数:
/* empty()可以检测stack内是否为空,true为空,false为非空 */ if(st.enpty()==true)
- size()函数:
/* 通过size()函数来获得栈的元素个数 */ st.size();
八、algorithm头文件下的常用函数
- max()/min()/abs():
max(x,y); //返回x,y中的最大值 min(x,y); //返回x,y中的最小值 abs(x); //返回x的绝对值
- swap():
swap(x,y); //交换x和y的值
- reverse():
//可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内进行反转 reverse(it,it2); /*数组反转*/ int a[10]={1,2,3,4,5,6,7,8,9,0}; reverse(a,a+4); /*容器反转*/ string str="3843264872"; reverse(str.begin(),str.begin()+9);
- next_permutation():
/*next_permutation()全排列*/ int a[3]={1,2,3}; do{ printf("%d %d %d\n",a[0],a[1],a[2]); }while(next_permutation(a,a+3)); /* 123 132 213 231 312 321 */
- fill():
/* fill()可以把数组或容器中的某一段区间赋值为某个值 */ fill(a,a+4,233);
- sort():
/* sort(元素首地址(必填),尾地址的下一个地址(必填),比较函数(非必填)) 比较函数如果不填,则默认为增序排列 结构体排序 bool cmp(node a,node b){ return a.x<b.x; } */