0

I got a struct that a user can define what string values go in where. I've tried ordering it alphabetically but had no luck with what research I found online. I was hoping if one of use can see where im going wrong

libraries being used : iostream, string, fstream and algorithm

struct House
{
    int room_num;               
    string person;      
};

struct compare_by_word
{
    bool operator()(const House& lhs, const House& rhs)
    {
        return lhs.person < rhs.person;
    }
};

I get errors on this line, by the way im using visual studios 2010

  void asc_order()
    {
        sort(data.begin(), data.end(), compare_by_word());
//for loop will be displayed here to show table
    }

Errors I get:

Error: Identifier data is undefined

struct compare_by_word Error: type name is not allowed

1
  • data should be declared. We assumed this code is just a snippet. for demonstration. You need to have something like vector<House> data; in your code somewhere in side asc_order before calling sort. Hopefully filling it with some Houses. Commented Mar 31, 2013 at 21:50

4 Answers 4

2

You're passing the type as comparator. You need to pass object of compare_by_word as comparator to sort.

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

Comments

1

You need to pass an instance of compare_by_word. This is done by calling its constructor:

std::sort(data.begin(), data.end(), compare_by_word());
//                                  ^^^^^^^^^^^^^^^^^

Live Demo

I also see that you're not compiling with any headers that introduce an object with a begin or end method. These are commonly used in vectors and other dynamic containers. So I think you should try passing the address range instead as feasible alternative:

std::size_t len = sizeof(data)/sizeof(*data);

std::sort(data, data + len, compare_by_word());

Live Demo

Or if you're compiling in C++11 you can pass a lambda callback in place of an explicit functor and use the begin and end library functions instead of the address range:

using std::begin;
using std::end;

std::sort(begin(data), end(data), [] (House const& lhs, House const& rhs)
{
    return lhs.person < rhs.person;
});

Live Demo

6 Comments

You should... reorder the elements in your example. It works great, but it doesn't really show that the sort is doing anything.
@Xymostech Yeah, maybe I should mix them up a bit. :)
thanks for that david! Never used OO programming before so sorry about newbish stuff, Just wondering if you could show me a live example with std::size_t len = sizeof(data)/sizeof(*data); std::sort(data, data + len, compare_by_word()); Thanks, when i tried it it tells me data is undefined =/
@Bobski Sure give me a second. :)
Here you go. Basically std::size_t is an unsigned integer type used for holding the size of things.
|
0

An alternative would be

struct compare_by_word
{
    bool operator()(const House& lhs, const House& rhs)
    {
        return lhs.person < rhs.person;
    }
} compare_by_word;  // Here.

1 Comment

to the answers provided before me
0

There is an error in sort statement.Replace it by:

sort(&data[0],&data[5],compare_by_word());

DEMO AT IDEONE

Comments

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.