comparator not called in std::map operations??

Discussion in 'C++' started by qazmlp, Jul 4, 2004.

  1. qazmlp

    qazmlp Guest

    The following code is taken from:
    http://www.sgi.com/tech/stl/Map.html

    As the keytype is 'const char*' in the std::map object, there is a
    special comparator function used here. But, when I run this program,
    I never get "Inside comparator" printed. What exactly is happening?

    I am just wondering how the insertion( via std::map::eek:perator[]()) and
    std::map::find() works properly without the comparator function
    getting used.

    Kindly clarify!
    --------------------------------------
    struct ltstr
    {
    bool operator()(const char* s1, const char* s2) const
    {
    std::cout << "Inside comparator\n" ;
    return strcmp(s1, s2) < 0;
    }
    };

    int main()
    {
    map<const char*, int, ltstr> months;

    months["january"] = 31;
    months["february"] = 28;
    months["march"] = 31;
    months["april"] = 30;
    months["may"] = 31;
    months["june"] = 30;
    months["july"] = 31;
    months["august"] = 31;
    months["september"] = 30;
    months["october"] = 31;
    months["november"] = 30;
    months["december"] = 31;

    cout << "june -> " << months["june"] << endl;
    map<const char*, int, ltstr>::iterator cur = months.find("june");
    map<const char*, int, ltstr>::iterator prev = cur;
    map<const char*, int, ltstr>::iterator next = cur;
    ++next;
    --prev;
    cout << "Previous (in alphabetical order) is " << (*prev).first <<
    endl;
    cout << "Next (in alphabetical order) is " << (*next).first << endl;
    }
    --------------------------------------------------------------
    qazmlp, Jul 4, 2004
    #1
    1. Advertising

  2. qazmlp

    Buster Guest

    qazmlp wrote:
    > The following code is taken from:
    > http://www.sgi.com/tech/stl/Map.html
    >
    > As the keytype is 'const char*' in the std::map object, there is a
    > special comparator function used here. But, when I run this program,
    > I never get "Inside comparator" printed. What exactly is happening?


    Your compiler and/or standard library implementation is broken.

    --
    Regards,
    Buster.
    Buster, Jul 4, 2004
    #2
    1. Advertising

  3. qazmlp

    David Harmon Guest

    On 3 Jul 2004 19:39:03 -0700 in comp.lang.c++,
    (qazmlp) wrote,
    >special comparator function used here. But, when I run this program,
    >I never get "Inside comparator" printed. What exactly is happening?


    Works for me including "Inside comparator" messages with Digital Mars
    C++ compiler and STLPORT, free download from http://www.digitalmars.com
    David Harmon, Jul 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,009
    robert
    Feb 11, 2006
  2. Peter Jansson
    Replies:
    5
    Views:
    6,263
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    404
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    996
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    1,981
    James Kanze
    Dec 21, 2008
Loading...

Share This Page