|
|
| sort a 'std::vector' containing classes/structures? |
|
| Author |
Message |
jimgong

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
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
Visual C++11
|
| |
|
| |
 |
Brian Kramer

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
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.
|
| |
|
| |
 |
Jim at HK

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
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.
|
| |
|
| |
 |
Marius Bancila

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
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
|
| |
|
| |
 |
valikac

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
Be careful when storing objects in STL containers by value without any custom copy constructors. You need one for deep copy.
valikac
|
| |
|
| |
 |
Brian Kramer

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
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.
|
| |
|
| |
 |
Marius Bancila

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
|
| |
 |
Brian Kramer

|
Posted: Visual C++ Language, sort a 'std::vector' containing classes/structures? |
Top |
|
| |
 |
| |
|