Using Iterator in Queue

D

Donos

Hello

I have a Queue which is declared as,

std::queue<unsigned char> m_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 char> m_Itr;

But it's not working.

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

Thanks.
 
M

Markus Moll

Hi
I have a Queue which is declared as,

std::queue<unsigned char> m_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 char> m_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 said:
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
 
D

Donos

But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned char> m_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?
 
M

Markus Moll

Hi
But still i can use a Queue to push data into it, right?

I don't quite understand...
ie; std::queue<unsigned char> m_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
 
D

Donos

But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned char> m_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?
 
R

Rolf Magnus

Donos said:
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.
 
G

Guest

But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned char> m_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.
 
J

James Kanze

But still i can use a Queue to push data into it, right?
ie; std::queue<unsigned char> m_Queue;
I tried declaring the Iterator as ,
std::deque<unsigned char>::iterator m_Itr;
Why?


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() ;

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top