2019年3月9日训练日记

  • 心得感受

经历了一个寒假,年前学的东西已经不记得多少了,说起来很是惭愧...现在学的东西(STL)对我来说完完全全是“新东西”,即使上课认真听,到做题的时候依旧没有什么思路,所以这么多天了,这一套题也才做了四五道......

我选这个课程的原因也很简单,想学点儿真本事。为了逼自己一把,没有退这门课,虽然我并不擅长这些,但我相信自己只要肯付出就一定可以。

  •  STL简单应用

set

头文件: #include <algorithm>

sort(begin, end);

sort(begin, end, cmp);

例如: int num[ ] = {1,5,6,2,9};          

1) sort(num, num + 5);//默认从小到大排序   

 2) bool cmp(int a, int b)

{       

 return a > b;   

 }   

 sort(num, num + 5, cmp); //num[] = {9,6,5,2,1};

优先队列

优先队列就是STL的#include<queue> 里面的 priority_queue,它的原理就是通过堆,如下图


头文件: #include <queue>

定义:priority_queue <data_type> priority_queue_name;   

 如:priority_queue <int> q;//默认是大顶堆

操作:     

  1. q.push(elem) 将元素elem置入优先队列  
  2. q.top() 返回优先队列的下一个元素   
  3. q.pop() 移除一个元素   
  4. q.size() 返回队列中元素的个数   
  5. q.empty() 返回优先队列是否为空

升序 : priority_queue<int, vector<int>, greater<int> > list;    

降序 : priority_queue<int, vector<int>, less<int> > list;
但要注意最后那两个 > 不能靠在一起,和>>引起 冲突

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define pow2(a) ((a)*(a))
#define dist2(x, y) (pow2(x) + pow2(y))
struct coord
{
    int x, y;
    const bool operator<(const coord &b)const
    {
        return (dist2(x, y) < dist2(b.x, b.y));
    }
};
int main()
{
priority_queue<coord> s;
coord a;
    a.x = 3, a.y = 2;
    s.push(a);
    a.x = 1, a.y = 2;
    s.push(a);
    a.x = 2, a.y = 2;
    s.push(a);
    cout << "Size: " << s.size() << endl;
    cout << "Top: " << s.top().x << ", " << s.top().y << endl;
    s.pop();
    cout << "Top: " << s.top().x << ", " << s.top().y << endl;
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值