473,378 Members | 1,438 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,378 software developers and data experts.

Using Iterator in Queue

Hello

I have a Queue which is declared as,

std::queue<unsigned charm_Queue;

I push data into this queue.

Now i want to take the data out of this queue using Iterator.

I tried declaring the Iterator as,

std::iterator<unsigned charm_Itr;

But it's not working.

Can anyone tell me how to declare an Iterator and take data out of a
Queue?

Thanks.

Oct 12 '07 #1
7 22648
Hi

Donos wrote:
I have a Queue which is declared as,

std::queue<unsigned charm_Queue;

I push data into this queue.

Now i want to take the data out of this queue using Iterator.
std::queue is not itself a container, but an adaptor (a facade) around a
container offering a limited interface. As such, std::queue does not offer
iterators.
I tried declaring the Iterator as,

std::iterator<unsigned charm_Itr;
There is a std::iterator template, but it does not do what you think it
does. Furthermore, iterators are not runtime-polymorphic, so there cannot
be a general iterator class for arbitrary containers. Instead, the iterator
class is always tightly bound to the corresponding container class. E.g.
there is std::vector<T>::iterator, std::list<T>::iterator and so on.
But it's not working.
No, it shouldn't.
Can anyone tell me how to declare an Iterator and take data out of a
Queue?
No way. If you want to iterate over the queue, you do not want a std::queue
but rather one of the containers that allow addition and removal at both
ends. Those would be std::list and std::deque.

Markus

Oct 12 '07 #2
But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned charm_Queue;

I tried declaring the Iterator as ,

std::deque<unsigned char>::iterator m_Itr;
then,

m_Itr = m_Queue.front(); // ERROR C2679

And this gives an error.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'unsigned char' (or there is no acceptable conversion)

Any idea?

Oct 12 '07 #3
Hi

Donos wrote:
But still i can use a Queue to push data into it, right?
I don't quite understand...
ie; std::queue<unsigned charm_Queue;
This looks good.
I tried declaring the Iterator as ,

std::deque<unsigned char>::iterator m_Itr;
then,

m_Itr = m_Queue.front(); // ERROR C2679
front() returns the first element, not an iterator to the first element. The
latter is returned by begin(). So it should read:

std::deque<unsigned char>::iterator m_Itr = m_Queue.begin();

Markus

Oct 12 '07 #4
But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned charm_Queue;

I tried declaring the Iterator as ,

std::deque<unsigned char>::iterator m_Itr;
then,

m_Itr = m_Queue.front(); // ERROR C2679

And this gives an error.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'unsigned char' (or there is no acceptable conversion)

Any idea?

Oct 12 '07 #5
Donos wrote:
But still i can use a Queue to push data into it, right?
Yes, that's what queues are for. There is just no need to use an iterator
for that.

Oct 12 '07 #6
On 2007-10-12 17:42, Donos wrote:
But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned charm_Queue;
If you can live with not having any iterators to its elements and want
to use it just like a queue then it is perfect for the job.

--
Erik Wikström
Oct 12 '07 #7
On Oct 12, 5:42 pm, Donos <dongu...@gmail.comwrote:
But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned charm_Queue;
I tried declaring the Iterator as ,
std::deque<unsigned char>::iterator m_Itr;
Why?
then,
m_Itr = m_Queue.front(); // ERROR C2679
And this gives an error.
error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'unsigned char' (or there is no acceptable conversion)
Queues are meant for queueing, not iterating. You can't get an
iterator into a queue, and if you need one, you shouldn't be
using a queue. And since there are no iterators for a queue,
you can't declare one.

What's wrong with simply:

unsigned char ch = m_Queue.front() ;

?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 14 '07 #8

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

Similar topics

16
by: newsock | last post by:
What differences between queue, deque and priority_queue? And under what situations choose one of them to use? Thanks for your help!
4
by: Jeronimo Bertran | last post by:
I have a multithreaded application that manipulates a Queue: protected Queue outputQueue; when I add elements to the queue I use a critical section private void Add(OutputRecord record) {...
3
by: kelkel | last post by:
Hi all, I got a question here which about using queue. i have create a class of queue. Public Sub Add(ByVal data As Object) SyncLock Me _Data.Enqueue(data) Monitor.Pulse(Me) End SyncLock End...
1
by: Ty Moffett | last post by:
I am trying to write a little app that will perform unattended installations of various software packages. I have a text file, each line is a string containing the complete command to start a...
6
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do...
1
by: Henrik Goldman | last post by:
Hello, I would like to know if it's legal to insert one or more items into a std::map while traversing it from begin() to end(). E.g. for (it = mymap.begin(); it != mymap.end(); it++) {...
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
1
by: Ralph Wiggum | last post by:
I'm planning on pulling data from a webservice and push the data on to a buffer/queue for each response. This should happen in a thread, while another thread parses the data (heavy) and writes them...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.