使用priority_queue [英] Using priority_queue

查看:85
本文介绍了使用priority_queue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用带指针的priority_queue?我问b $ b问的原因是我想要一个包含对象指针的数据结构,而不是
对象的副本。另外,我希望它们按优先级排序。在相同的

注释中,有没有办法编写运算符:


bool运算符<(Foo * foo1,Foo * foo2); ?


在运算符内部,指针将被解除引用然后进行比较。

不可能这样做吗?如果没有,我的解决方案就是在比较它们之前简单地取消引用任何指针。我也不想每次我从优先级队列中弹出一些内容时都要抛弃const。

(另外一点是,用C ++编译有多贵?我知道在Java中铸造

太经常会强奸你的应用程序的性能。)如果我想要这样的功能,我会被要求做自己的数据

结构?

解决方案

2004年7月16日星期五14:00:44 -0400,阿吉拉尔,詹姆斯

< jf ** @ cec.NOBOTSwustl.edu>写道:

有没有办法使用带指针的priority_queue?




我问的原因是我想要一个包含对象指针的数据结构,
不是
对象的副本。另外,我希望它们按优先级排序。在
同样的说明中,有没有办法写一个算子:

bool operator<(Foo * foo1,Foo * foo2);?


不,你不能为内置类型重载运算符。至少有一个

参数必须是一个类。

在运算符内部,指针将被解除引用然后进行比较。
它不是有可能做到这一点?如果没有,我的解决方案是在比较之前简单地取消引用我的指针。每次我从我的优先级队列中弹出一些东西时,我也不想抛弃const。
(旁注来说,用C ++编译有多贵?我知道在Java中
经常会强迫你的应用程序的性能。)如果我想要这样的功能,我是否会被要求制作我自己的数据结构?




除了dynamic_cast之外,在C ++中非常高效,大部分时间它都是无操作的,但即使编译器必须应用一些偏移量

在编译时计算出来。我猜dynamic_cast类似于Java,在

任何情况下都是运行时计算。


一些priority_queue代码(未经过测试,只是为了给你一个想法)


#include< queue>

#include< functional>


struct CompareFooPtr:public std: :binary_function< Foo *,Foo *,bool>

{

bool operator()(Foo * x,Foo * y)const

{

返回* x< * y;

}

};


typedef std :: priority_queue< Foo *,std :: vector< Foo * >,CompareFooPtr>

MyQueue;


MyQueue a_queue;


john


On Fri,2004年7月16日11:12:47 -0700,John Harrison写道:

周五,2004年7月16日14:00:44 - 0400,Aguilar,James

(另一方面,在C ++中投入的成本有多高?我在Java中知道过于频繁地投射会严重影响你的应用程序的性能。)


除了dynamic_cast之外,在C ++中非常高效,大部分时间都是非操作,但即使编译器必须应用一些在编译时得到的偏移量时间。




有些演员表需要执行代码才能完成

转换。以下程序有三个演员阵容。它为三个转换器构造函数调用了三个

。 (编译为

臭名昭着的g ++ 2.96。)


#include< iostream>


A类

{

public:

A(){std :: cout<< " A\\\
英寸; }


void foo()const {}

};


B级

{

public:


B(){std :: cout<< B default\\\
; }

B(A const&){std :: cout<< " B(A)\\\
英寸; }


运算符A()const

{

返回A();

}


void foo()const {}

};


int main()

{

B b0;

A const& a = static_cast< A const&>(b0);


B const& b1 = static_cast< B const&>(a);


B const& b2 =(B)a;

}


Ali


2004年7月16日星期五12:12:12 -0700,Ali Cehreli< ac ****** @ yahoo.com>写道:

周五,2004年7月16日11:12:47 -0700,John Harrison写道:

2004年7月16日星期五14:00:44 -0400,阿吉拉尔,詹姆斯

(旁注,在C ++中铸造的成本有多高?我知道在Java中经常铸造可能会强奸你的应用程序''性能。)


除了dynamic_cast之外,在C ++中进行强制转换非常有效,大部分时间都是无操作的,但是甚至如果编译器必须应用一些在编译时计算出来的偏移量。



有些强制转换需要执行代码才能完成
转换。以下程序有三个演员阵容。它向三个构造函数进行三次
调用以进行转换。 (用臭名昭着的g ++ 2.96编译。)

#include< iostream>

A类
{
公开:
A(){std :: cout<< " A\\\
英寸; }

void foo()const {}
};

B班
{
公开:
B(){std :: cout<< B default\\\
; B(A const&){std :: cout<< " B(A)\\\
英寸; }
运算符A()const
{
返回A();
}

void foo()const {}
};

int main()
{b / B B0;
一个const& a = static_cast< A const&>(b0);
B const& b1 = static_cast< B const&>(a);

B const& b2 =(B)a;
}
阿里




好​​的好点。并且还有从double到int等的强制转换。

也会导致一些代码被执行。


我实际上只考虑演员表从指向一种类型的指针到指向另一种相关类型的指针,因为这与Java中发生的最相似,但我应该说清楚。


john


Is there any way to use a priority_queue with pointers? The reason why I
ask is that I want a data structure that contains pointers to objects, not
copies of objects. Also, I want them to be sorted by priority. On the same
note, then, is there a way to write an operator:

bool operator <(Foo* foo1, Foo* foo2);?

Inside the operator, the pointers would be dereferenced then compared. Is
it not possible to do this? If not, my solution would be to simply
dereference any pointers I had before comparing them. I also want to not
have to cast away const every time I pop something from my priority queue.
(On a side note, how expensive is casting in C++? I know in Java casting
too often can really rape your application''s performance.) If I want
functionality like this, am I going to be required to make my own data
structure?

解决方案

On Fri, 16 Jul 2004 14:00:44 -0400, Aguilar, James
<jf**@cec.NOBOTSwustl.edu> wrote:

Is there any way to use a priority_queue with pointers?
Yes
The reason why I
ask is that I want a data structure that contains pointers to objects,
not
copies of objects. Also, I want them to be sorted by priority. On the
same
note, then, is there a way to write an operator:

bool operator <(Foo* foo1, Foo* foo2);?
No, you cannot overload operator for built in types. At least one of the
parameters must be a class.

Inside the operator, the pointers would be dereferenced then compared.
Is
it not possible to do this? If not, my solution would be to simply
dereference any pointers I had before comparing them. I also want to not
have to cast away const every time I pop something from my priority
queue.
(On a side note, how expensive is casting in C++? I know in Java casting
too often can really rape your application''s performance.) If I want
functionality like this, am I going to be required to make my own data
structure?



Apart from dynamic_cast casting is very efficient in C++, most of the time
its a no-op, but even if the compiler has to apply some offset that is
worked out at compile time. I guess dynamic_cast is similar to Java, in
any case its a runtime computation.

Some priority_queue code (not tested, just to give you the idea)

#include <queue>
#include <functional>

struct CompareFooPtr : public std::binary_function<Foo*, Foo*, bool>
{
bool operator()(Foo* x, Foo* y) const
{
return *x < *y;
}
};

typedef std::priority_queue<Foo*, std::vector<Foo*>, CompareFooPtr>
MyQueue;

MyQueue a_queue;

john


On Fri, 16 Jul 2004 11:12:47 -0700, John Harrison wrote:

On Fri, 16 Jul 2004 14:00:44 -0400, Aguilar, James

(On a side note, how expensive is casting in C++? I know in Java
casting too often can really rape your application''s performance.)

Apart from dynamic_cast casting is very efficient in C++, most of the
time its a no-op, but even if the compiler has to apply some offset that
is worked out at compile time.



Some casts do require code to be executed in order to complete the
conversion. The following program has three casts. It makes three
calls to three constructors for the conversions. (Compiled with the
infamous g++ 2.96.)

#include <iostream>

class A
{
public:
A() { std::cout << "A\n"; }

void foo() const {}
};

class B
{
public:

B() { std::cout << "B default\n"; }
B(A const &) { std::cout << "B(A)\n"; }

operator A() const
{
return A();
}

void foo() const {}
};

int main()
{
B b0;
A const & a = static_cast<A const &>(b0);

B const & b1 = static_cast<B const &>(a);

B const & b2 = (B)a;
}

Ali


On Fri, 16 Jul 2004 12:12:12 -0700, Ali Cehreli <ac******@yahoo.com> wrote:

On Fri, 16 Jul 2004 11:12:47 -0700, John Harrison wrote:

On Fri, 16 Jul 2004 14:00:44 -0400, Aguilar, James

(On a side note, how expensive is casting in C++? I know in Java
casting too often can really rape your application''s performance.)


Apart from dynamic_cast casting is very efficient in C++, most of the
time its a no-op, but even if the compiler has to apply some offset that
is worked out at compile time.



Some casts do require code to be executed in order to complete the
conversion. The following program has three casts. It makes three
calls to three constructors for the conversions. (Compiled with the
infamous g++ 2.96.)

#include <iostream>

class A
{
public:
A() { std::cout << "A\n"; }

void foo() const {}
};

class B
{
public:

B() { std::cout << "B default\n"; }
B(A const &) { std::cout << "B(A)\n"; }
operator A() const
{
return A();
}

void foo() const {}
};

int main()
{
B b0;
A const & a = static_cast<A const &>(b0);
B const & b1 = static_cast<B const &>(a);

B const & b2 = (B)a;
}

Ali



OK good point. And as well there are casts from double to int etc. etc.
which also cause some code to be executed.

I was actually thinking only of casts from a pointer to one type to a
pointer to another releated type since that was the most similar to what
happens in Java, but I should have made that clear.

john


这篇关于使用priority_queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆