Using custom std::set comparator

Posted by Omry on Stack Overflow See other posts from Stack Overflow or by Omry
Published on 2010-04-12T09:05:03Z Indexed on 2010/04/12 9:13 UTC
Read the original article Hit count: 1005

Filed under:
|

I am trying to change the default order of the items in a set of integers to be lexicographic instead of numeric, and I can't get the following to compile with g++:

file.cpp:

bool lex_compare(const int64_t &a, const int64_t &b) 
{
    stringstream s1,s2;
    s1 << a;
    s2 << b;
    return s1.str() < s2.str();
}

void foo()
{
    set<int64_t, lex_compare> > s;
    s.insert(1);
    ...
}

I get the following error:

error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Compare, class _Alloc> class std::set’
error:   expected a type, got ‘lex_compare’

what am I doing wrong?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl