Rwokk
Rwokk

Reputation: 33

C++ vector<Struct> sorting not working

I'm currently trying to write a function that sorts a vector of Entry items, which are defined here in my header file.

struct Entry{
    string word;
    int count;
};

Basically, each Entry has a string and an int. What I'm trying to do is sort a vector<Entry> by each Entry's count value in descending order. I've tried using std::sort in the .cpp file:

bool intcomp(const Entry &lhs, const Entry &rhs){
    return lhs.count < rhs.count;
}

void SortedByCount(std::ostream &out) const{
    std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
}

but the compiler then spits out a huge wall of errors, like this one

/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/bits/stl_heap.h:247:12: note:
  in instantiation of function template specialization
  'std::__push_heap<__gnu_cxx::__normal_iterator<Entry *const *,
  std::vector<Entry *, std::allocator<Entry *> > >, long, Entry *>'
  requested here
  std::__push_heap(__first, __holeIndex, __topIndex,
       ^

I'm very lost on what to do, so any pointers will be appreciated.

EDIT: The header file contains the struct for Entry and its constructor, as well as the prototype for intcomp and SortedByCount(std::ostream &out) const, while the .cpp file contains the definition of intcomp and SortedByCount(std::ostream &out) const. I get this error:

 reference to non-static member function must
 be called
 std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
                                                 ^

Is it because the intcomp() method isn't static? Or what else could it be? Thanks again.

Upvotes: 1

Views: 860

Answers (3)

Dainius
Dainius

Reputation: 1852

Works just fine:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

struct Entry {
    std::string word;
    int count;

    Entry(const std::string& s, int c) : word(s), count(c) {}
};

std::vector<Entry> vocabulary;

bool intcomp(const Entry &lhs, const Entry &rhs) {
    return lhs.count < rhs.count;
}

void sortedbycount(std::ostream &out) {
    std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
}

void main()
{
    vocabulary.push_back(Entry("second", 2));
    vocabulary.push_back(Entry("firs", 1));
    vocabulary.push_back(Entry("third", 3));

    sortedbycount(std::cout);

    for (const auto& e : vocabulary)
    {
        std::cout << e.word << " " << e.count << "\n";
    }
}

Upvotes: 0

Mr.C64
Mr.C64

Reputation: 42984

To sort vector content using custom criteria, you can use a C++11 lambda:

sort(entries.begin(), entries.end(),
    [](const Entry& a, const Entry& b)
    {
        return a.Count > b.Count;
    }
);

Compilable source code sample follows (live here on Ideone):

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

struct Entry 
{
    string Word;
    int Count;

    Entry(const string& word, int count)
        : Word(word), Count(count)
    {
    }
};

int main() {
    vector<Entry> entries = {{"hello", 2}, {"world", 8}, {"hi", 20}, {"connie", 10}};

    sort(entries.begin(), entries.end(),
        [](const Entry& a, const Entry& b)
        {
            return a.Count > b.Count;
        }
    );

    for (const auto& e : entries)
    {
        cout << e.Word << ": " << e.Count << '\n';
    }
}

Output:

hi: 20
connie: 10
world: 8
hello: 2

Upvotes: 0

Ishamael
Ishamael

Reputation: 12795

Your vocabulary vector contains pointers to Entrys, not Entrys themselves. Change your comparator to the following:

bool intcomp(const Entry *lhs, const Entry *rhs){
    return lhs->count < rhs->count;
}

Upvotes: 3

Related Questions