Sorting STL vector

This is a discussion on Sorting STL vector within the C++ Programming forums, part of the General Programming Boards category; How do I sort a STL vector containing my own class. I have a class called CObject and a vector ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Question Sorting STL vector

    How do I sort a STL vector containing my own class.

    I have a class called CObject and a vector
    vector<CObject> Objects

    In CObject is a function called GetYPos().
    I need to sort the vector so that the Objects with the lowest YPos are first.

    I tried using std::sort with the operator < overloaded in CObject but it doesn't work.

    Is there any other solutions?

    Thanks

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The overloaded operator < and std::sort method is the easiest. What kind of errors were you getting?

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    class MyObject
    {
    	int m_x;
    public:
    	MyObject& operator=(int x)
    	{
    		m_x = x;
    		return *this;
    	}
    	bool operator<(const MyObject& rhs)
    	{
    		return (m_x < rhs.m_x);
    	}
    	void Print()
    	{
    		std::cout << m_x << " ";
    	}
    };
    
    int main(void)
    {
    	std::vector<MyObject> vec(4);
    	int i;
    
    	vec[0] = 8;
    	vec[1] = 5;
    	vec[2] = 0;
    	vec[3] = 2;
    
    	for(i = 0;i < vec.size();++i)
    	{
    		vec[i].Print();
    	}
    
    	std::cout << std::endl;
    
    	std::sort(vec.begin(),vec.end());
    
    	for(i = 0;i < vec.size();++i)
    	{
    		vec[i].Print();
    	}
    
    	std::cout << std::endl;
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Unhappy

    passing `const CObject' as `this' argument of `bool CObject::operator<(const CObject&)' discards qualifiers

    stl_algo.h:2118: instantiated from `void std::__introsort_loop(_RandomAccessIter, _RandomAccessIter, _Size) [with _RandomAccessIter = __gnu_cxx::__normal_iterator< CObject*, std::vector< CObject, std::allocator< CObject> > >, _Size = int]'

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off, does your overloaded < operator work outside of the sort function like if you just tested it in main?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    10
    Yes, it does.

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Hmmmm.... post the relevant code, that might help.

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The member function must be const.
    It should look like this.

    Code:
    bool CObject::operator<(const CObject&) const
    My second post on the following thread answers a very similiar question, and shows why the function signature looks like it does.

    http://www.cprogramming.com/cboard/s...const+operator
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an STL Vector of pointers
    By Beamu in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2009, 05:25 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21