Page 1 of 1

How sort an container with boost::shared_ptr

Posted: Tue Jun 23, 2009 10:36 pm
by juanmafont
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 ?

Posted: Wed Jun 24, 2009 10:20 am
by Frank

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);

Posted: Wed Jun 24, 2009 12:06 pm
by Muetdhiver
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