473,795 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reserve with std::priority_q ueue

In using std::priority_q ueue, I'm concerned about the expense of
memory allocation and copying as the priority_queue grows large.
std::vector has reserve() to address this concern, though there
doesn't seem to be anything like this for priority_queue. Also, I
don't know anything about how priority_queue is implemented
(specifically, MSVC++ 6.0), so maybe I don't have anything to worry
about. Guidance, please?

Regards,
Ryan
Jul 22 '05 #1
3 4476

"Tino" <ti****@yahoo.c om> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
In using std::priority_q ueue, I'm concerned about the expense of
memory allocation and copying as the priority_queue grows large.
std::vector has reserve() to address this concern, though there
doesn't seem to be anything like this for priority_queue. Also, I
don't know anything about how priority_queue is implemented
(specifically, MSVC++ 6.0), so maybe I don't have anything to worry
about. Guidance, please?

Regards,
Ryan


priority_queue is not necessarily based on a vector, e.g. here's a priority
queue that uses a deque.

priority_queue< int, deque<int> > my_queue

I'd use a priority_queue based on a deque if you are worried about
reallocations as the queue grows.

For implementation, just look in the header file. priority_queue is llikely
to be pretty simple since most of the work is done by the underlying
container and other STL algorithms.

john
Jul 22 '05 #2
Tino wrote:
In using std::priority_q ueue, I'm concerned about the expense of
memory allocation and copying as the priority_queue grows large.
std::vector has reserve() to address this concern, though there
doesn't seem to be anything like this for priority_queue. Also, I
don't know anything about how priority_queue is implemented
(specifically, MSVC++ 6.0), so maybe I don't have anything to worry
about. Guidance, please?


You could provide your own "underlying " container type for the
std::priority_q ueue, perhaps containing a std::vector. You custom
container could have its member vector reserve an arbitrary amount of
memory.
Jul 22 '05 #3
On 28 May 2004 07:47:54 -0700, ti****@yahoo.co m (Tino) wrote:
In using std::priority_q ueue, I'm concerned about the expense of
memory allocation and copying as the priority_queue grows large.
std::vector has reserve() to address this concern, though there
doesn't seem to be anything like this for priority_queue. Also, I
don't know anything about how priority_queue is implemented
(specificall y, MSVC++ 6.0), so maybe I don't have anything to worry
about. Guidance, please?

Regards,
Ryan


std::vector guarantees that the memory it uses is contiguous so when
the allocated chunk of memory needs to be expanded a new chunk of
memory large enough for the whole vector needs to be allocated and all
the contents of the vector copied into the new memory before the old
memory is released.

The other containers do not have to be contiguous so they can leave
the old contents where they are and just allocate a new chunk of
memory large enough to hold the new object being added to the
container. They do not have to copy the existing contents since these
are left in the old memory.

Hence they do not need a reserve function like vector. Reserve is
useful when you know the size of the vector in advance and can
allocate all the memory needed in one chunk at the beginning and so
avoid having to copy large pieces of the vector.

rossum

--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6357
by: Tino | last post by:
Hopefully a simple question: What is the best/safest/fastest way to clear/make empty a std::priority_queue? Thanks, Ryan
7
3128
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
2
1547
by: red floyd | last post by:
I know it's generally bad from to derive from STL containers. That said, here's my question: I am using std::priority_queue<>. However, I need to be able to access the underlying container; specifically, I need to be able to get at begin() and end() for it. Josuttis (10.3.3) shows that the compare operator and the container are *protected* members of std::priority_queue. This would lead me to believe that it was intended for...
11
2097
by: Aguilar, James | last post by:
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...
9
28783
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt find methods for these which suprises me as both should be easily solvable with access to the underlying vector. Did I just miss these methods? Can I somehow access the vector to get . begin() .end() and .find()?
18
5533
by: J.M. | last post by:
I would like to use a data structure (e.g. from the STL) that always allows me to retrieve the largest element. (I want to push in elements, and remove the largest, push in further elements, etc.) It seems a priority_queue from the STL would work fine. However at some point, I am finished (even though the queue is not empty) and want to throw away the rest of the elements. It seems, I have to do this using: while (!Q.empty()) Q.pop(); ...
6
11259
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without emptying it? Right now I'm using the following code: while (!pq.empty()) { cout << setw(3) << pq.top().first << ": " << pq.top().second <<
8
4162
by: thomas | last post by:
priority_queue usually uses the greater<intpredicate function. But as you know, we don't always use priority_queue<int>. Actually we may need the "priority_queue<pair<int,int>, vector<pair<int,int, cmphp;" thing. My question is how should I write the "cmp" function? I tried this one:
24
2579
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest timestamp. // just a wrapper around priority_queue pq = new MyPriorityQueue(); // I generate 10 events w/ a random timestamp for (int i = 0; i < 10; i++) { MyEvent *event = new MyEvent();
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.