Solved

Sort pointers in container

Posted on 2002-07-01
1
182 Views
Last Modified: 2010-04-01
I have a container with pointers that I'm trying to sort.

How do you sort a container of pointers?

class XObject
{
public:
int xyz;
};

list<XObject*> ptrListOfXObject;

How do I sort ptrListOfXObject in order of xyz data member?
0
Comment
Question by:ROWANBRIGHTON
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 30

Accepted Solution

by:
Axter earned 270 total points
ID: 7121600
Here is some example code.

#include <list>
#include <iostream>

class myObject{
public:
    myObject(){m_Val = 0;}
    int getVal() const //Constant here
    {
          return m_Val;
    }
    void setVal(int pVal){m_Val = pVal;}
private:
    int m_Val;
};

template<>
struct std::greater<myObject*>
{
public:
    virtual bool operator()(const myObject*& lhs, const myObject*& rhs) const
     {
          return lhs->getVal() > rhs->getVal();
     }
};

class ProcessOrder
{
public:
     void SomeFunction();
private:
     
     typedef std::list<myObject *> MYLIST;
     MYLIST myList;
};

void ProcessOrder::SomeFunction()
{
     const Qty = 4;
     myObject My_myObject[Qty];
     int SomeNum[Qty] = {3, 8, 7, 2};
     for (int i = 0;i < Qty;++i)
     {
        My_myObject[i].setVal(SomeNum[i]);
        myList.push_back(&My_myObject[i]);
     }
     
     myList.sort(std::greater<myObject*>());
     
     
     for (MYLIST::iterator x = myList.begin();x != myList.end();++x)
     {
        std::cout << (*x)->getVal() << std::endl;
     }
}

int main(int argc, char* argv[])
{
     ProcessOrder MyProcessOrder;
     MyProcessOrder.SomeFunction();
     system("pause");
     return 0;
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question