Thread: wat to use?

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The reason for this is that std::map's operator< will treat our keys as numeric values, and therfor compare the address where our pointers are pointing. But we want to sort them alphabetically - and therefor we should write our own operator< for this.
    It is only proper to use std::less when dealing with numeric values, imo.
    The obvious solution is not to use C style strings

    std::less is not only proper with numeric values but with any types that define operator< (which is what less calls), e.g std::string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by anon View Post
    The obvious solution is not to use C style strings

    std::less is not only proper with numeric values but with any types that define operator< (which is what less calls), e.g std::string.
    true, but as written earlier - I'm only writing examples. and this time it was to show that you cannot always trust std::map's default usage of std::less - even though it can be trusted when dealing with the "correct" variable-types.

    What I meant with "numeric types" in my post above was (I can't find the English word for it.. but) more "basic" types (like int, double, char, etc) that doesn't have a operator< already defined. (Hope you understand what I mean)
    Of course 'char *' is a "basic type" when reading the above, but maybe you understand what I'm trying to say?

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes. I just wanted to point out that std::pair already has operator< (which does the exact opposite of your comparison functor) and as long as both types in the pair have a usable operator< (like char) you don't need to write your own for something like std::pair<char, char>.

    And the fact that C style strings require all that extra work to make them work with STL should discourage their use (in such contexts). You don't normally want to let a C style string close to standard containers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone tell me wat is the problem??
    By peter_hii in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2006, 07:03 AM
  2. wat is the easiest way to create a palindrome
    By Krazen in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 08:06 AM
  3. wat does this function do?
    By gamett711221 in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2005, 01:56 PM
  4. wat is the use of sizeof(int)?
    By zell in forum C Programming
    Replies: 3
    Last Post: 01-24-2005, 05:27 AM