0

I have class and this class contains a number. And I have a vector contains object pointer of class. And I want to sort that objects according to their numbers. How can I do this? Thanks for answers.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Course
{
public:
    Course (int code, string const& name) : name(n), code(c) {}
    int getCourseCode() const { return code; }
    string const& getName() const { return name; }

private:
    string name;
    int code;
};

int main()
{
    vector<Course*> cor;
    vector<Course*>::iterator itcor;

    cor.push_back(new Course(3,"first"));
    cor.push_back(new Course(2,"sekond"));
    cor.push_back(new Course(4,"third"));
    cor.push_back(new Course(1,"fourth"));
    cor.push_back(new Course(5,"fifth"));  
    sort (cor.begin(), cor.end());
    for (itcor=cor.begin(); itcor!=cor.end(); ++itcor) {
        cout << *itcor << ' ';
    }
}

For example when I want the sort the objects they are being sorted according to their adresses.

4
  • "And I have a vector contains objects of class." - No, it doesn't. Your vector contains pointers to objects, not objects. Commented Oct 25, 2012 at 16:06
  • 1
    @user1559792 - to "accept" an answer is to click on the check-mark next to an answer. To "upvote" an answer is to click on the upward-facing triangle next to an answer. I don't think you have accepted any answers yet. Commented Oct 25, 2012 at 16:10
  • Oh I didn't know that. I thought the "was that helpful" button is the correct one. Commented Oct 25, 2012 at 16:12
  • Here's an example of a question you asked which recieved an excellent answer that you did not accept. Please go through your question history and accept answers where appropriate. You'll get less resistance when you ask a question that way. stackoverflow.com/questions/13050436/… Commented Oct 25, 2012 at 16:15

2 Answers 2

4

You'll need to provide a custom comparator class or function to the std::sort method to make it not sort by addresses.

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

where comp can be defined as:

bool comparer(const Course* x, const Course* y) { /*compare*/ }
//or
struct comparer {
  bool operator() (const Course* x, const Course* y) { /*compare*/ }
} comparerObject;

and call sort as:

std::sort(cor.begin(), cor.end(), comparer);   //method alternative

or

std::sort(cor.begin(), cor.end(), comparerObject);   //class alternative

That, or don't keep pointers in the vector. From the code you posted, it's not clear that you actually need pointers:

vector<Course> cor;

should be enough.

Sign up to request clarification or add additional context in comments.

1 Comment

or std::sort(cor.begin(), cor.end(), comparer());
0

You can do this in three ways:

1) Overload the < operator, and call std::sort algorithm. The code will look like this:

bool operator<(Course *a, Course *b) const {
  // do comparison
  return A_BOOL_VALUE;
}

std::sort(array_of_courses.begin(),array_of_courses.end());

The first way is wrong, as you can't overload the < operator in pointers.

2) Create a compare function, and then call the second version of std::sort. The code looks like this:

bool compare(Course *a,Course *b) {
  // do comparison
  return A_BOOL_VALUE;
}

std::sort(array_of_courses.begin(),array_of_courses.end(),compare);

3) Create a compare class, which has it's () operator overloaded, and then call the third verion of std::sort. The code:

struct Compare {
  bool operator()(Course *a, Course *b) {
    // do comparison
    return A_BOOL_VALUE;
  }
};

std::sort(array_of_courses.begin(),array_of_courses.end(),Compare);

Note: the sort function is found at the algorithm header file.

4 Comments

Did you notice that OP has a vector of pointers, not a vector of objects?
those signatures need to take Course* for the OP's code, and I assume they should be Course const & otherwise, to match operator<?
-1 Besides the vector being a vector of pointers, you don't pass the class name as parameter to sort.
You can't overload bool operator<(Course *a, Course *b), there's already a built-in < operator for pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.