priority_queue
hello,u
i see a problem:
#include<iostream>
#include<list>
#include<queue>
int main()
{
std::priority_queue<int,std::vector<int>,
std::greater<int> >intpQueue;
intpQueue.push(400);
intpQueue.push(100);
intpQueue.push(500);
intpQueue.push(300);
intpQueue.push(200);
std::cout<<"Values removed from priority queue:"
<<std::endl;
int size=intpQueue.size();
for(int x=0;x<size;++x)
{
//intpQueue.pop();
std::cout<<std::endl;
std::cout<<intpQueue.top()<<std::endl;
intpQueue.pop();
}
return 0;
}
as it expects ,its output is 500 400 300 200 100 but when i change something ,the matter comes worse,that is
i change the header file #include<list> into #include<vector>
and the std::vector to std::list it will come out plenty of errors
such as a undefinited list and std doesnot contain list etc.
and
#include<iostream>
#include<deque>
#include<stack>
int main()
{
std::stack<int,std::deque<int> > intstack;
std::cout<<"Values pushed onto stack:"
<<std::endl;
for(int x=1;x<11;++x)
{
intstack.push(x*100);
std::cout<<x*100<<std::endl;
}
std::cout<<"Values poped from stack:"
<<std::endl;
int size=intstack.size();
for( x=0;x<size;++x)
{
std::cout<<intstack.top()<<std::endl;
intstack.pop();
}
return 0;
}
in this code i can change the std::deque to std::list or std::vector as i can
could one tell me something about it ?
[1850 byte] By [
jolley] at [2007-11-18 21:40:40]

# 1 Re: priority_queue
i change the header file #include<list> into #include<vector>
and the std::vector to std::list it will come out plenty of errors
such as a undefinited list and std doesnot contain list etc.
I can tell you that before playing around with STL containers,
you should learn basic stuff as: "including headers - what is it good for?..." and "Templates"
when you use the standard templates library's containers, you have to include the appropriate headers
since the containers are template classes.
So, when using std::vector, include <vector>
and when using std::list, include <list>.
Another thing, please post you code using the code tag and disable smilies.
Regards,
Guy
Guysl at 2007-11-9 0:34:21 >

# 2 Re: priority_queue
thanks Guysl for your advice
and i will pay enough attention to my code tag and disable similies
thanks again
but in the first code ,we can get the same conclusion as you tell me
because it doesnot contain any header file about vector,the header file is #include<iostream>include<list>include<queue>
it doesnot contain #include<vector>at all!
# 5 Re: priority_queue
Originally posted by jolley
1 the reason why std::vector object canot create a std::queue
2 the reason why std::list object cannot create a std::priority_queue.
3 the reason why std::list and std::deque and atd::vector can create a std::stack
Well...what are your problems? The following is fine and compiles without any errors...
#include <deque>
#include <queue>
#include <vector>
std::vector<std::deque> > VectorOfDeques;
std::vector<std::priority_queue> > VectorOfQueues;
# 6 Re: priority_queue
Originally posted by Andreas Masur
Well...what are your problems? The following is fine and compiles without any errors...
#include <deque>
#include <queue>
#include <vector>
std::vector<std::deque> > VectorOfDeques;
std::vector<std::priority_queue> > VectorOfQueues;
now that you understand it ,you 'd better see my code again, after all ,my question is in the code .please do see it again ,and give me some helpful advice
thanks again