PQ使用

priority_queue默认用 < 比较元素,也就是数值大的优先级排列在最前面,最先出队列。那么怎样使它翻转输出呢,书中的例子只是简单的吧key本身的大小作为优先级来使用了,那么这里就要借助一个结构了,用来存储key和priority
第一次代码比较复杂:
pq.cpp

#include<cstdio>
#include<queue>

using namespace std;

class NODE {
    public:
        char ch;
        int  freq;

        bool operator>(const NODE n1) const
        {
            return (this->freq > n1.freq);
        }

        NODE(char c, int n)
        {
            ch = c;
            freq = n;
        }
};

int main()
{
    priority_queue< NODE, vector<NODE>, greater<NODE> > pq;

    pq.push(NODE('a', 8));
    pq.push(NODE('b', 4));
    pq.push(NODE('c', 5));

    printf("%d %c\n", pq.top().freq, pq.top().ch);
    pq.pop();
    printf("%d %c\n", pq.top().freq, pq.top().ch);

    return 0;
}

第二次简单了一些,pq_v2.cpp:

#include<cstdio>
#include<queue>

using namespace std;

class NODE {
    public:
        char ch;
        int  freq;

        // cos the default priority is from biger one to smaller one
        bool operator<(const NODE n1) const
        {
            return (this->freq > n1.freq);// priority retorate.
        }

        NODE(char c, int n)
        {
            ch = c;
            freq = n;
        }
};

int main()
{
    priority_queue<NODE> pq;

    pq.push(NODE('a', 8));
    pq.push(NODE('b', 4));
    pq.push(NODE('c', 5));

    printf("%d %c\n", pq.top().freq, pq.top().ch);
    pq.pop();
    printf("%d %c\n", pq.top().freq, pq.top().ch);

    return 0;
}


这里用的是模板库提供的pq的模板,它的定义时这个样子的:

In their implementation in the C++ Standard Template Library, priority queues take three template parameters:

1
2
template < class T, class Container = vector<T>,
           class Compare = less<typename Container::value_type> > class priority_queue;

Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.
  • Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and aand b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b).
    The priority_queue object uses this expression when an element is inserted or removed from it (using push orpop, respectively) to grant that the element popped is always the greatest in the priority queue.
In the reference for the  priority_queue  member functions, these same names ( T Container  and  Compare ) are assumed for the template parameters.

默认的话只需要提供数据的类型即可,对于基本的数据类型,他是按照less的顺序入队列,出队列的时候是优先级比较大的先出;它默认使用的容器是vector;然后还有less实际上是一个函数对象,它的内部重载了()操作符,相当于一个<号,这里贴出greater的代码理解一下,

This class is derived from  binary_function  and is defined as:

1
2
3
4
template <class T> struct greater : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x>y;}
};

Members

bool operator() (const T& x, const T& y)
Member function returning the result of the comparison  x>y.
可以看到他是比较了x, y的大小,所以对于我们自己提供的类型,也要重载>或者<运算符就可以了,这也就是2中的重载的作用。


bool operator<(const NODE n1)后面少了一个const,以下是G++出错的提示,一满屏了快。。。:

xi@xi-virtual-machine:1053$ g++ huffman.cpp -o huffman
In file included from /usr/include/c++/4.6/queue:64:0,
                 from huffman.cpp:2:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Item]’:
/usr/include/c++/4.6/bits/stl_heap.h:305:4:   instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item, std::allocator<Item> > >, _Distance = int, _Tp = Item, _Compare = std::less<Item>]’
/usr/include/c++/4.6/bits/stl_heap.h:436:4:   instantiated from ‘void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item, std::allocator<Item> > >, _Compare = std::less<Item>]’
/usr/include/c++/4.6/bits/stl_queue.h:391:9:   instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = Item, _Sequence = std::vector<Item, std::allocator<Item> >, _Compare = std::less<Item>]’
huffman.cpp:31:26:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const Item’ as ‘this’ argument of ‘bool Item::operator<(Item)’ discards qualifiers [-fpermissive]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
说明:运行 program 文件夹中的 launcher.exe 即可使用。 Powerquest Partition Magic后被norton收购改叫Norton Partition Magic,最新版8.05有5年没更新。 而 Paragon Partition Manager 公司名是 Paragon。版本更新比较块,各个WinPE系统里面基本有它的7.0 Server版,不过Bug多点。7.0做有中文ntfs分区调整大小和合并容易出错。8.5以后的版本支持到vista的 NTFS 5.x,据说已经能针对不同语言代码页中文系统下无损调整分区不会出错了。至于只需要重新分区的 7.0 一直够用。最新版Pro版就是9.0。 介绍:Paragon Partition Manager 是一套磁盘管理软件,是目前为止最好用的磁盘管理工具之一,能够优化磁盘使应用程序和系统速度变得更快,不损失磁盘数据下调整分区大小,对磁盘进行分区,并可以在不同的分区以及分区之间进行大小调整、移动、隐藏、合并、删除、格式化、搬移分区等操作,可复制整个硬盘资料到分区,恢复丢失或者删除的分区和数据,无需恢复受到破坏的系统就可磁盘数据恢复或拷贝到其他磁盘。能够管理安装多操作系统,方便的转换系统分区格式,也有备份数据的功能,支持 Vista 系统下操作。 Paragon Partition Manager 9.0 有着直觉的图形使用介面和支持鼠标操作。主要功能包括:能够不损失硬盘资料下对硬盘分区做大小调整、能够将 NTFS 文件系统转换成 FAT、FAT32 或 FAT32 文件系统转换成 FAT 文件系统、支持制作、格式化、删除、复制、隐藏、搬移分区、可复制整个硬盘资料到其它分区、支持长文件名、支持 FAT、FAT32、NTFS、HPFS、Ext2FS 分区和大于 8GB 大容量硬盘。 可以很容易地改变分区大小、参数等,并且不会造成数据丢失,支持改变 FAT16/32 和 NTFS 的簇大小,可在 DOS 或任何 Windows 版本下访问 NTFS, Ext2FS 分区数据,支持 NTFS 分区的 MFT 优化,可转换 NTFS 分区的版本(XP <-> 2000 <-> NT) 。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值