CPP标准模板库の打开方式
stl容器的遍历(使用vector数组进行演示):
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
int main()
{
int n; cin >> n;
vector<int> pq;
for(int i = 1; i <= n; i++)
{
int inst;
cin >> inst;
pq.push_back(inst);
}
for(auto i = pq.begin() ; i != pq.end() ; i++)
{
cout << *i << ' ';
}
cout << '\n';
for(vector<int>::iterator it = pq.begin() ; it != pq.end() ; it++)
{
cout << *it << ' '; //必须进行解引用因为迭代器返回的是一个指针
}
cout << '\n';
for(auto i : pq)
{
cout << i << ' ';
}
cout << '\n';
// for(vector<int>::iterator it : pq)//这样子写是错误的
// {
// cout << *it << ' ';
// }
// cout << '\n';
return 0;
}
注意pair的大小比较,是先对first进行比较,如果相等再对second进行比较。
比如下面的:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int main()
{
int n; cin >> n;
priority_queue< pair<int ,int > , vector<pair<int ,int > > , greater<pair<int , int> > > pq;
for(int i = 1 ; i <= n ; i++)
{
int x , y ; cin >> x >> y;
pq.push(make_pair(x , y));
}
while(pq.size())
{
cout << " (x , y) = " << pq.top().first << ' ' << pq.top().second << endl;
pq.pop();
}
return 0;
}
输入:
6
12 921
203 -34
12 310
12 -114514
54 0
143 6999
输出:
(x , y) = 12 -114514
(x , y) = 12 310
(x , y) = 12 921
(x , y) = 54 0
(x , y) = 143 6999
(x , y) = 203 -34
之后便是next_permutation全排列stl
之后便是lowerbound二分查找stl,可以或最左端的把(大概)
*lower_bound( stk.begin() , stk.end() , a[i] ) = a[i];
priority的从小到大与从大到小
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
signed main()
{
priority_queue<int , vector<int> , greater<int>> heap1;
int n = 3;
for(int i = 1; i <= n; i++)
{
int x; cin >> x;
heap1.push(x);
}
while(heap1.size())
{ cout << heap1.top() << ' '; heap1.pop(); }
puts("");
priority_queue<int , vector<int>> heap2;
n = 3;
for(int i = 1; i <= n; i++)
{
int x; cin >> x;
heap2.push(x);
}
while(heap2.size())
{ cout << heap2.top() << ' '; heap2.pop(); }
puts("");
}
operator,这种方式以结构体当中的w作为权重从大到小进行排序:
bool operator < (const node &q) const
{
return q.w < w;
}
这种方式以结构体当中的w作为权重从小到大进行排序:
bool operator < (const node &q) const
{
return w < q.w;
}
评论