473,772 Members | 3,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy elements from a pq to a vector

I have a priority_queue that stores its elements in a vector.
What is the fastest way to copy all the elements of the
priority queue to this vector. Right now I use pop() to pop
all the elements one by one and copy it to the output vector.

I was thinking maybe one can inherit the priority_queue class
to get access to the internal vector so that copy would be fast.
Any ideas?

Thanks,
--j

Apr 2 '06 #1
2 8515
John wrote:
I have a priority_queue that stores its elements in a vector.
What is the fastest way to copy all the elements of the
priority queue to this vector. Right now I use pop() to pop
all the elements one by one and copy it to the output vector.

I was thinking maybe one can inherit the priority_queue class
to get access to the internal vector so that copy would be fast.
Any ideas?


No need to inherit. Just use the 'top' and copy as you would an array
of type T:

priority_queue< T> myq;
....
vector<T> outputvec(myq.s ize());
....
std::copy(&(myq .top()), &(myq.top()) + myq.size(), &outputvec[0]);

Since the underlying container of your queue is a vector, its continuity
can be used to extract contents. It's not a good idea if you expect to
change the underlying container to deque at any point.

V
--
Please remove capital As from my address when replying by mail
Apr 2 '06 #2
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:1o******** *************** *******@comcast .com...
: John wrote:
: > I have a priority_queue that stores its elements in a vector.
: > What is the fastest way to copy all the elements of the
: > priority queue to this vector. Right now I use pop() to pop
: > all the elements one by one and copy it to the output vector.
: >
: > I was thinking maybe one can inherit the priority_queue class
: > to get access to the internal vector so that copy would be fast.
: > Any ideas?
:
: No need to inherit. Just use the 'top' and copy as you would an array
: of type T:
:
: priority_queue< T> myq;
: ...
: vector<T> outputvec(myq.s ize());
: ...
: std::copy(&(myq .top()), &(myq.top()) + myq.size(), &outputvec[0]);
:
: Since the underlying container of your queue is a vector, its continuity
: can be used to extract contents. It's not a good idea if you expect to
: change the underlying container to deque at any point.

Hm, however I'm afraid that this will not be equivalent to pop()-ing the
items one by one, if the order of the elements matters: a priority queue
keeps its items sorted in non-linear fashion.
So at best the items in the output vector would need to be sorted
again in a second pass...

What I would like to question, though, is: why is a priority_queue used
in the first place?
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Apr 3 '06 #3

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

Similar topics

9
9915
by: Gunnar G | last post by:
Is there anything like vector in STL, that performes deep copy of the elements it contains? I hope this will appear in future releases of STL :)
3
1626
by: Nudge | last post by:
I am slightly confused, so please keep that in mind if I make only little sense. Assume I have a base class: class Base { ... }; and two derived classes: class Int : public Base { ... int foo; };
14
1717
by: Jinjun Xu | last post by:
Hi, I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it's correct. ////////////// start int *p1 = new int; // .... // set the values of p1
18
2305
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
1
2127
by: utab | last post by:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <iterator> using std::cout; using std::vector; using std::string; using std::endl;
27
3392
by: JoeC | last post by:
I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My gdata will work if I do: gdata = this->get(lp); But that is not what I want to do I get some cryptic errors when I write the code like that is commented out. 49 C:\Documents and Settings\Work\My Documents\C++\Dungeon Adventure2\gaphic.cpp...
5
2393
by: Fred | last post by:
Hi: I've got the following request: (suppose the required head file is included) vector<stringvec; // .......... // push some items into vec string str; // here I want to copy some range of elements from vec to str, for example, from vec
13
2470
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
7
2160
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but they do requrie much more memory than a simple pointer.
0
9620
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
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...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2850
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.