#include <cstdlib>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int myints[]= {1,3,5,2,4,6};
class Node{
public:
Node(int x):m_nX(x){}
int getX() const{
return m_nX;
}
bool operator<(const Node& other) const{
return m_nX < other.getX();
}
bool operator>(const Node& other) const{
return m_nX > other.getX();
}
private:
int m_nX;
};
vector<Node> nodeVec;
void print(const vector<int>& vec){
for(int i=0;i<vec.size();i++)
cout<<vec[i]<<" ";
cout<<endl;
}
void print(const vector<Node>& nodeVec){
for(int i=0;i<nodeVec.size();i++)
cout<<nodeVec[i].getX()<<" ";
cout<<endl;
}
void testSortInt(){
vector<int> vec(myints, myints+6);
sort(vec.begin(),vec.end(),less<int>());
print(vec);
sort(vec.begin(),vec.end(),greater<int>());
print(vec);
}
void testSortWithNode(){
sort(nodeVec.begin(),nodeVec.end(),less<Node>());
print(nodeVec);
sort(nodeVec.begin(),nodeVec.end(),greater<Node>());
print(nodeVec);
}
void initNodeVec(){
Node a(1);
Node b(3);
Node c(5);
Node d(2);
Node e(4);
Node f(6);
nodeVec.push_back(a);
nodeVec.push_back(b);
nodeVec.push_back(c);
nodeVec.push_back(d);
nodeVec.push_back(e);
nodeVec.push_back(f);
}
void print(priority_queue<int, vector<int>, less<int> >& queue){
while(!queue.empty()){
cout<<queue.top()<<" ";
queue.pop();
}
cout<<endl;
}
void print(priority_queue<int, vector<int>, greater<int> >& queue){
while(!queue.empty()){
cout<<queue.top()<<" ";
queue.pop();
}
cout<<endl;
}
void testPriorityQueueWithInt(){
priority_queue<int, vector<int>, less<int> > lessQueue(myints,myints+6);
print(lessQueue);
priority_queue<int, vector<int>, greater<int> > greaterQueue(myints,myints+6);
print(greaterQueue);
}
void print(priority_queue<Node, vector<Node>, less<Node> >& queue){
while(!queue.empty()){
cout<<queue.top().getX()<<" ";
queue.pop();
}
cout<<endl;
}
void print(priority_queue<Node, vector<Node>, greater<Node> >& queue){
while(!queue.empty()){
cout<<queue.top().getX()<<" ";
queue.pop();
}
cout<<endl;
}
void testPriorityQueueWithNode(){
priority_queue<Node, vector<Node>, less<Node> > lessQueue(nodeVec.begin(),nodeVec.end());
print(lessQueue);
priority_queue<Node, vector<Node>, greater<Node> > greaterQueue(nodeVec.begin(),nodeVec.end());
print(greaterQueue);
}
int main()
{
testSortInt();
initNodeVec();
testSortWithNode();
testPriorityQueueWithInt();
nodeVec.clear();
initNodeVec();
testPriorityQueueWithNode();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int myints[]= {1,3,5,2,4,6};
class Node{
public:
Node(int x):m_nX(x){}
int getX() const{
return m_nX;
}
bool operator<(const Node& other) const{
return m_nX < other.getX();
}
bool operator>(const Node& other) const{
return m_nX > other.getX();
}
private:
int m_nX;
};
vector<Node> nodeVec;
void print(const vector<int>& vec){
for(int i=0;i<vec.size();i++)
cout<<vec[i]<<" ";
cout<<endl;
}
void print(const vector<Node>& nodeVec){
for(int i=0;i<nodeVec.size();i++)
cout<<nodeVec[i].getX()<<" ";
cout<<endl;
}
void testSortInt(){
vector<int> vec(myints, myints+6);
sort(vec.begin(),vec.end(),less<int>());
print(vec);
sort(vec.begin(),vec.end(),greater<int>());
print(vec);
}
void testSortWithNode(){
sort(nodeVec.begin(),nodeVec.end(),less<Node>());
print(nodeVec);
sort(nodeVec.begin(),nodeVec.end(),greater<Node>());
print(nodeVec);
}
void initNodeVec(){
Node a(1);
Node b(3);
Node c(5);
Node d(2);
Node e(4);
Node f(6);
nodeVec.push_back(a);
nodeVec.push_back(b);
nodeVec.push_back(c);
nodeVec.push_back(d);
nodeVec.push_back(e);
nodeVec.push_back(f);
}
void print(priority_queue<int, vector<int>, less<int> >& queue){
while(!queue.empty()){
cout<<queue.top()<<" ";
queue.pop();
}
cout<<endl;
}
void print(priority_queue<int, vector<int>, greater<int> >& queue){
while(!queue.empty()){
cout<<queue.top()<<" ";
queue.pop();
}
cout<<endl;
}
void testPriorityQueueWithInt(){
priority_queue<int, vector<int>, less<int> > lessQueue(myints,myints+6);
print(lessQueue);
priority_queue<int, vector<int>, greater<int> > greaterQueue(myints,myints+6);
print(greaterQueue);
}
void print(priority_queue<Node, vector<Node>, less<Node> >& queue){
while(!queue.empty()){
cout<<queue.top().getX()<<" ";
queue.pop();
}
cout<<endl;
}
void print(priority_queue<Node, vector<Node>, greater<Node> >& queue){
while(!queue.empty()){
cout<<queue.top().getX()<<" ";
queue.pop();
}
cout<<endl;
}
void testPriorityQueueWithNode(){
priority_queue<Node, vector<Node>, less<Node> > lessQueue(nodeVec.begin(),nodeVec.end());
print(lessQueue);
priority_queue<Node, vector<Node>, greater<Node> > greaterQueue(nodeVec.begin(),nodeVec.end());
print(greaterQueue);
}
int main()
{
testSortInt();
initNodeVec();
testSortWithNode();
testPriorityQueueWithInt();
nodeVec.clear();
initNodeVec();
testPriorityQueueWithNode();
system("PAUSE");
return EXIT_SUCCESS;
}