hi guys.
i'm supposed to write a program about priority queue.
in this case, i have to read the data from a text file and store it into a queue and sorted it into priority queue.
and also, i have to use 2 classes for that.
anyone have link that's helpful for me?
i'm totally confused about this.
i've just learned about classes about 1 week ago.
until now my basic understanding about classes is weak.
but suddenly my teacher gave me this assignment. :(
thanks.
I'm nt just helping i happened to be on the page. A priorty queue is very simailar to a plain queue: eleements can be added only at the back and removed only from the front. the differerence is that wit a priority queue, the element that is ready to be removed from the front is always the one with highest priority, not the one that was inserted first.
to moorecm
i do have. but the textbook explain it separately.
e.g. queue, fstream input&output, classes.
suddenly my teacher gave me a task that require me to combine those 3.
i'm totally confused about that :(
to gratrstone
what i don't understand is what i've just mentioned above.
for each case, i do understand (not 100% of course).
but now the problem is i have to combine it together in 1 task. so confused.
anyway, thanks for your help. at least i got the logic :)
to moorecm
that's my problem. :(
i don't understand how to relate those with each other.
if i have time maybe i'll understand by go through it step by step.
but since the time given isn't much, so i need your help guys. :(
until now, i'm still trying. trial and error.
to salaamesaleem
eeerrrr. sorry. i don't have time for that. i already have my own task. :(
it's better if you make your own topic.
it also help people who're already expert in c++ easier to find and solve your problem. :)
This website has information on the implementation of priority_queue. http://www.cplusplus.com/reference/stl/priority_queue/
salaamesaleem, you should read this before you even consider trying a post like that again: http://www.cplusplus.com/forum/beginner/1/
Essentially, you'd engender an fstream to read in data line by line (or whatever unit you want) and then insert that data into a queue as you read it in. Keep in mind that a queue is a FIFO structure. I don't really see why you'd suddenly switch to priority queue but that's the basic idea.
It's very simple--you just have to *learn* those things. You might have to think a little bit beyond what is handed to you in order to combine the concepts. It takes work; there are no shortcuts.
to tummychouw
i've read that source already. but still, i don't get it.
anyway, thanks for the link, tummychouw. :)
to moorecm
yup. thanks for the advice, moorecm.
i know that there's no shortcuts.
the only thing that annoy me is that the due date is on 14th February.
i just don't want to miss any moments on that day. :)
Priority queue
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition.
This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely.
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it must be accessible through random access iterators and it must support the following operations:
* front()
* push_back()
* pop_back()
Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particular priority_queue class, the standard container class template vector is used.
Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and pop_heap when appropriate.
In their implementation in the C++ Standard Template Library, priority queues take three template parameters:
1
2
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
Where the template parameters have the following meanings:
* T: Type of the elements.
* Container: Type of the underlying container object used to store and access the elements.
* Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b).
The priority_queue object uses this expression when an element is inserted or removed from it (using push or pop, respectively) to grant that the element popped is always the greater in the priority queue.
In the reference for the priority_queue member functions, these same names (T, Container and Compare) are assumed for the template parameters.
Member functions
(constructor) Construct priority queue (public member function)
empty Test whether container is empty (public member function)
size Return size (public member function)
top Access top element (public member function)
push Insert element (public member function)
pop Remove top element (public member function)
FIFO queue
queues are a type of container adaptors, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access it elements. Elements are pushed into the "back" of the specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:
* front()
* back()
* push_back()
* pop_front()
Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.
In their implementation in the C++ Standard Template Library, queues take two template parameters:
template < class T, class Container = deque<T> > class queue;
Where the template parameters have the following meanings:
* T: Type of the elements.
* Container: Type of the underlying container object used to store and access the elements.
In the reference for the queue member functions, these same names are assumed for the template parameters.
Member functions
(constructor) Construct queue (public member function)
empty Test whether container is empty (public member function)
size Return size (public member function)
front Access next element (public member function)
back Access last element (public member function)
push Insert element (public member function)
pop Delete next element (public member function)
to gratrstone
actually i haven't studied about template before, so it's forbidden for me to use that.
anyway, thanks for your help. :)
Edit:
Anyone know the logic of this priority queue?
I've tried this way:
Read the data 1 by 1, and if the sex is 'Female' then insert it to a queue 'q1', else if the sex is 'Male, insert it to a queue 'q2'.
than display the q1 and q2 so that i can get the priority queue.
but my teacher only allow me to use 1 queue.
anyone know? i'm stucked. :(