468,513 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

how to know whether a queue has contained an element

HI all,

I use <queueof STL. Before pushing an element into the queue, how to
check whether the queue has contained the element? i.e. I want the
queue has no duplicated elements. Is ther Contains() function?

e.g.

#include <queue>

int main(void)
{
queue<intq;
q.push(1);
q.push(2);

// q.push(1); // since q now contains "1", q should not push "1"
again.

// something like
// if (!q.Contains(1))
// {
// q.push(1);
// }

return 0;
}
many thanks indeed!
Dec 17 '07 #1
2 9806
On Dec 17, 11:23 am, "sg71.che...@gmail.com" <sg71.che...@gmail.com>
wrote:
HI all,

I use <queueof STL. Before pushing an element into the queue, how to
check whether the queue has contained the element? i.e. I want the
queue has no duplicated elements. Is ther Contains() function?

e.g.

#include <queue>

int main(void)
{
queue<intq;
q.push(1);
q.push(2);

// q.push(1); // since q now contains "1", q should not push "1"
again.

// something like
// if (!q.Contains(1))
// {
// q.push(1);
// }

return 0;

}

many thanks indeed!
you can use deque instead of queue if you want data structure to
behave as queue. Otherwise you can use set.

deque<intq;
q.push_back(1);
q.push_back(2);

deque<int>::iterator it = find(q.begin(),q.end(),1);
if(it != q.end())
{
q.push_back(1);
}
Dec 17 '07 #2
On 2007-12-17 15:39:13 -0500, "sg*********@gmail.com"
<sg*********@gmail.comsaid:
>
I believe queue is very suitable when implementing FIFO (providing
only pop() and push() interfaces), so it is the case for my project.
However, it is indeed lack of "searchablity". I have switched to
deque, working a little bit carefully with pop_front() and push_back()
to achieve the same FIFO.
That's not quite the right answer, though, since, as you say, you have
to work carefully with a deque. Use a deque, because it does the things
you need, but wrap it in a class that exposes only the operations you
need. Use std::queue as an example of this approach.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jeronimo Bertran | last post: by
4 posts views Thread by Sachin Jagtap | last post: by
5 posts views Thread by Dan H. | last post: by
8 posts views Thread by Jack | last post: by
4 posts views Thread by j_depp_99 | last post: by
19 posts views Thread by =?ISO-8859-1?Q?Nordl=F6w?= | last post: by
5 posts views Thread by randysimes | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.