473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8307
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.size());
....
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.********@comAcast.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.size());
: ...
: 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
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
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 { ......
14
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; ...
18
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...
1
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
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...
5
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...
13
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.