jimgong

Hi,

I want to sort a vector contain some data using std::sort. Here are the sample codes.

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

class MyData
{
public:
int m_iData;
string m_strSomeOtherData;
};

bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs)
{
return lhs.m_iData < rhs.m_iData;
}

int main()
{
// Create list
vector myvector;

// Add data to the vector
MyData data;
data.m_iData = 3;
myvector.push_back(data);
data.m_iData = 1;
myvector.push_back(data);

// Sort the vector using predicate and std::sort
std::sort(myvector.begin(), myvector.end(), MyDataSortPredicate);

// Dump the vector to check the result
for (vector::const_iterator citer = myvector.begin();
citer != myvector.end(); ++citer)
{
cout << (*citer).m_iData << endl;
}

return 0;
}

This program works well. However, the function "MyDataSortPredicate" is a global function. How could I make it a member function of the "MyData" class Thanks.

-Jim


Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Brian Kramer

You can move that function into the class and declare it as static. (static bool MyDataSortPredicate() {...}). Nothing else changes (i.e. it does not have a this pointer), except you now pass &MyData::MyDataSortPredicate into std::sort. Note that VS2005 now requires & to get addresses of functions, whereas VS2003 just made it optional.





Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Jim at HK

Brian Kramer wrote:

You can move that function into the class and declare it as static. (static bool MyDataSortPredicate() {...}). Nothing else changes (i.e. it does not have a this pointer), except you now pass &MyData::MyDataSortPredicate into std::sort. Note that VS2005 now requires & to get addresses of functions, whereas VS2003 just made it optional.



Thank you very much.




Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Marius Bancila

You can also write a function object:

class MyDataSorter

{

public:

  bool operator()(const MyData& lhs, const MyData& rhs)

  {

     return lhs.m_iData < rhs.m_iData;

  }
};

 

std::sort(myvector.begin(), myvector.end(), MyDataSorter());

PS: did not test the code






Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

valikac

Be careful when storing objects in STL containers by value without any custom copy constructors. You need one for deep copy.

valikac




Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Brian Kramer

Marius, functors are cool!  I use them in my bit vector implementation (i.e. enumerate bits, apply a function over the words, etc).  The compiler inlines aggressively, so the functor layer goes away.



Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Marius Bancila

For Jim, here is an article about function objects, http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c11449/

For Brian, when you said Marcus, I think you meant Marius, right






Re: Visual C++ Language sort a 'std::vector' containing classes/structures?

Brian Kramer

Corrected.