sorting vector that holds object
-
hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v
-
hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v
// Console.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; // My object. class MyObject { public: MyObject(int Index) : m_Index(Index) {} int m_Index; }; // Output to stream. ostream& operator<<(ostream &s, const MyObject &o) { s << o.m_Index; return s; } // A functor to compare two 'MyObject's based on the member 'm_Index'. struct MyObjectLess { bool operator()(const MyObject &l, const MyObject &r) const { return l.m_Index < r.m_Index; } }; int main(int argc, char* argv[]) { // Our collection. vector objects; // Fill the collection. objects.push_back(MyObject(3)); objects.push_back(MyObject(1)); objects.push_back(MyObject(4)); objects.push_back(MyObject(1)); objects.push_back(MyObject(5)); objects.push_back(MyObject(9)); // Print the collection. cout << "Before sorting:" << endl; copy(objects.begin(), objects.end(), ostream_iterator(cout, "\n")); cout << endl; // Sort them based on the 'm_Index' member variable. sort(objects.begin(), objects.end(), MyObjectLess()); // Print the collection. cout << "After sorting:" << endl; copy(objects.begin(), objects.end(), ostream_iterator(cout, "\n")); cout << endl; return 0; } Steve
-
hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v
If you talk about STL library, then the sorting can be done with
sort
function defined inalgorithm
. You have to devine a "binary predicate", wich compares objects according to your needs. Solution depends on how you store objects in vector -- as values or as pointers. The bellow sample shows both cases:struct MyObject
{
MyObject(int x, int y)
{
mX = x; mY = y;
}// binary predicate for the case of storing values static bool ComparatorByX\_values(const MyObject & obj1, const MyObject & obj2) { return obj1.mX < obj2.mY; } // binary predicate for the case of storing pointers static bool ComparatorByX\_pointers(const MyObject \* obj1, const MyObject \* obj2) { return obj1->mX < obj2->mY; } int mX, mY;
};
int main()
{
{
// case 1: storing values.std::vector< MyObject > vector; vector.push\_back(MyObject(3, 20)); vector.push\_back(MyObject(2, 10)); vector.push\_back(MyObject(1, 30)); // sort by X std::sort(vector.begin(), vector.end(), MyObject::ComparatorByX\_values); // print X for(size\_t i = 0; i < vector.size(); ++i) { printf("%i\\n", vector\[i\].mX); } } { // case 2: storing pointers. std::vector< MyObject \* > vector; vector.push\_back(new MyObject(3, 20)); vector.push\_back(new MyObject(2, 10)); vector.push\_back(new MyObject(1, 30)); // sort by X std::sort(vector.begin(), vector.end(), MyObject::ComparatorByX\_pointers); for(size\_t i = 0; i < vector.size(); ++i) { printf("%i\\n", vector\[i\]->mX); } // (TODO: delete the objects using "delete" in a loop) }
}
Hope this will ispire you. -- modified at 10:46 Friday 2nd June, 2006
-
hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v
the object class must overload the comparison operators (
<
,>
,<=
,>=
) and then, simply callstd::sort(vec.beging(), vec.end());
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]