How sort an container with boost::shared_ptr

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply
juanmafont
In need of some credit
In need of some credit
Posts: 1
Joined: Tue Jun 23, 2009 10:16 pm

How sort an container with boost::shared_ptr

Post by juanmafont » Tue Jun 23, 2009 10:36 pm

Hello ¡¡¡
I'm updating my code, but i have found a problem.

Into de old code I have:

Code: Select all


----rawfile.h------
class Rawfile
{

  double shutterTime;
.....
friend bool operator<(const Rawfile& element1, const rawfile& element2);
....
}
-----end rawfile.h--------


-----rawfile.cpp--------
//Function called when sort of array of objects. The field used for sorting is shutterTime
bool operator<(const Rawfile& element1, const Rawfile& element2)
{
     return element1.shutterTime < element2.shutterTime;
}
-----end rawfile.cpp-------


---main.cpp---

typedef std::vector<Rawfile> vectorRawfile;

vectorRawfile filesraw;

for(.....
  {
  filesraw.push_back( new Rawfile(filename));
  }

//friend function defined into class Rawfile
sort(filesraw.begin(), filesraw.end());
And the sort is made ok




Now, Im trying to implement using boost:shared_ptr but I don't know how .

New Code

Code: Select all


class Rawfile
{
.....
}

typedef boost::shared_ptr<Rawfile> RawfilePtr;
typedef std::vector<RawfilePtr> vectorRawfile;

vectorRawfile filesraw;

for(.....
  {
  filesraw.push_back( RawfilePtr (new Rawfile(filename)));
  }

//friend function defined into class Rawfile
sort(filesraw.begin()->get(), filesraw.end()->());
And this is the problem: How define the sort function ?
and how call it ?

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Wed Jun 24, 2009 10:20 am

Code: Select all

static bool MySortFunc (const boost::shared_ptr<Rawfile>& l, const boost::shared_ptr<Rawfile>& r)
{
   return l->operator<(*r);
}

// Use it like this:

sort(filesraw.begin(), filesraw.end(), MySortFunc);

Muetdhiver
Super wx Problem Solver
Super wx Problem Solver
Posts: 323
Joined: Sun Jun 08, 2008 11:59 am
Location: Bordeaux, France

Post by Muetdhiver » Wed Jun 24, 2009 12:06 pm

I don't know where there is a relationship with wxWidgets ?
And above all: where did you get the idea of posting such a problem in a forum named "Discussion forum for wxWidgets C++ programming"....

:P
OS: Ubuntu 11.10
Compiler: g++ 4.6.1 (Eclipse CDT Indigo)
wxWidgets: 2.9.3

Post Reply