473,765 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Iterator in Queue

Hello

I have a Queue which is declared as,

std::queue<unsi gned 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<u nsigned 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 22698
Hi

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

std::queue<unsi gned 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<u nsigned 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>::i terator 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<unsi gned charm_Queue;

I tried declaring the Iterator as ,

std::deque<unsi gned 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<unsi gned charm_Queue;
This looks good.
I tried declaring the Iterator as ,

std::deque<unsi gned 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<unsi gned 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<unsi gned charm_Queue;

I tried declaring the Iterator as ,

std::deque<unsi gned 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<unsi gned 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<unsi gned charm_Queue;
I tried declaring the Iterator as ,
std::deque<unsi gned 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 objektorientier ter Datenverarbeitu ng
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
12693
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
3572
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) { lock(outputQueue) {
3
1789
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 Sub Public Function GetNext() As Object
1
1548
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 silent/unattended install. I have successfully read the entire file and each line gets added to the queue. On this line "myProcess.StartInfo.FileName(myQ.Dequeue)" i get an error: C:\Documents and Settings\tmoffett\My Documents\Visual Studio...
6
3404
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 this? thank vector<intIntVec; vector<int>::iterator intIterator; for(int i=0; i<10;i++) IntVec.push_back(i);
1
1730
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++) { if (condition)
1
2207
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 ) { vector<T>::iterator i; // ERROR, can't use iterator }
1
1973
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 to disk. Time is of the essence, so the goal is to run the webservice requests parallel to processing data in the queue. Do you have any tips on how to "drive" this? Can I put pop/push event handlers on the queue, event handlers on webservice...
0
9566
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
9393
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
10153
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
9946
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
8830
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
7371
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
6646
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
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

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.