Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sorting vector that holds object

sorting vector that holds object

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsalgorithmshelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    voorugonda prashanth
    wrote on last edited by
    #1

    hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v

    S V T 3 Replies Last reply
    0
    • V voorugonda prashanth

      hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      // 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

      1 Reply Last reply
      0
      • V voorugonda prashanth

        hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        If you talk about STL library, then the sorting can be done with sort function defined in algorithm. 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

        1 Reply Last reply
        0
        • V voorugonda prashanth

          hi all, please help me how can i sort (based on a object member variable ) a vector that holds object. Regards, Prashanth.v

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          the object class must overload the comparison operators ( <, >, <=, >= ) and then, simply call std::sort(vec.beging(), vec.end());


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups