Re: Is a std::map<> ordered?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Juha Nieminen

    Re: Is a std::map<> ordered?

    joseph cook wrote:
    A map is always sorted using std::less
    Not always. By default, yes, but you can specify other comparators, eg:

    std::map<int, int, std::greaterrev ersedMap;
  • acehreli@gmail.com

    #2
    Re: Is a std::map&lt;&gt ; ordered?

    On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
    joseph cook wrote:
    A map is always sorted using std::less
    >
      Not always. By default, yes, but you can specify other comparators, eg:
    >
    std::map<int, int, std::greaterrev ersedMap;
    Or at runtime:

    std::map<int, intmyMap(myPred icate);

    Ali

    Comment

    • Pete Becker

      #3
      Re: Is a std::map&lt;&gt ; ordered?

      On 2008-11-20 14:40:19 -0500, acehreli@gmail. com said:
      On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
      >joseph cook wrote:
      >>A map is always sorted using std::less
      >>
      >  Not always. By default, yes, but you can specify other comparators, e
      g:
      >>
      >std::map<int , int, std::greaterrev ersedMap;
      >
      Or at runtime:
      >
      std::map<int, intmyMap(myPred icate);
      >
      Not really. There's a third type argument to std::map which specifies
      the map's predicate type, with a default of std::less<T>. This
      constructor takes an argument with the same type as the template's
      predicate argument, so you can't pass arbitrary predicate objects. This
      constructor is only useful with a user-defined predicate type that can
      be initialized with something other than its default constructor.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • Rolf Magnus

        #4
        Re: Is a std::map&lt;&gt ; ordered?

        Pete Becker wrote:
        On 2008-11-20 14:40:19 -0500, acehreli@gmail. com said:
        >
        >On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
        >>joseph cook wrote:
        >>>A map is always sorted using std::less
        >>>
        >>Not always. By default, yes, but you can specify other comparators, e
        >g:
        >>>
        >>std::map<in t, int, std::greaterrev ersedMap;
        >>
        >Or at runtime:
        >>
        > std::map<int, intmyMap(myPred icate);
        >>
        >
        Not really. There's a third type argument to std::map which specifies
        the map's predicate type, with a default of std::less<T>. This
        constructor takes an argument with the same type as the template's
        predicate argument, so you can't pass arbitrary predicate objects. This
        constructor is only useful with a user-defined predicate type that can
        be initialized with something other than its default constructor.
        Well, basically that just does mean that it depends on a runtime value.
        Otherwise, you wouldn't need those constructor arguments in the first place.


        Comment

        • Juha Nieminen

          #5
          Re: Is a std::map&lt;&gt ; ordered?

          acehreli@gmail. com wrote:
          std::map<int, intmyMap(myPred icate);
          I don't think you can do that because the comparator template
          parameter is set by default to std::less, and unless myPredicate casts
          implicitly to type std::less, that won't work. You have to do it like:

          std::map<int, int, MyPredicateType myMap(myPredica te);

          If 'myPredicate' is a function, the syntax becomes awkward:

          std::map<int, int, bool(*)(int, int)myMap(myPre dicate);

          This becomes even more awkward if the key and data types of the map
          are something more complicated than int.

          The next standard will offer a tool to alleviate the problem:

          std::map<int, int, decltype(myPred icate)myMap(myP redicate);

          Comment

          • Pete Becker

            #6
            Re: Is a std::map&lt;&gt ; ordered?

            On 2008-11-21 06:34:27 -0500, Rolf Magnus <ramagnus@t-online.desaid:
            Pete Becker wrote:
            >
            >On 2008-11-20 14:40:19 -0500, acehreli@gmail. com said:
            >>
            >>On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
            >>>joseph cook wrote:
            >>>>A map is always sorted using std::less
            >>>>
            >>>Not always. By default, yes, but you can specify other comparators, e
            >>g:
            >>>>
            >>>std::map<int , int, std::greaterrev ersedMap;
            >>>
            >>Or at runtime:
            >>>
            >>std::map<in t, intmyMap(myPred icate);
            >>>
            >>
            >Not really. There's a third type argument to std::map which specifies
            >the map's predicate type, with a default of std::less<T>. This
            >constructor takes an argument with the same type as the template's
            >predicate argument, so you can't pass arbitrary predicate objects. This
            >constructor is only useful with a user-defined predicate type that can
            >be initialized with something other than its default constructor.
            >
            Well, basically that just does mean that it depends on a runtime value.
            Otherwise, you wouldn't need those constructor arguments in the first place.
            It means that std::map<int, intmyMap(myPred icate) is an error unless
            the type of myPredicate is std::less<int>, in which cast it's
            irrelevant. In particular, it is not a runtime replacement for
            std::map<int, int, std::greater<in t>>.

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            • Pete Becker

              #7
              Re: Is a std::map&lt;&gt ; ordered?

              On 2008-11-21 08:46:16 -0500, Juha Nieminen <nospam@thanks. invalidsaid:
              acehreli@gmail. com wrote:
              >std::map<int , intmyMap(myPred icate);
              >
              I don't think you can do that because the comparator template
              parameter is set by default to std::less, and unless myPredicate casts
              implicitly to type std::less, that won't work. You have to do it like:
              >
              std::map<int, int, MyPredicateType myMap(myPredica te);
              >
              If 'myPredicate' is a function, the syntax becomes awkward:
              >
              std::map<int, int, bool(*)(int, int)myMap(myPre dicate);
              Well, yes, and as we've seen in many Ginsu knife commercials, a normal
              knife can't slice a tomato. The way to write this code is, of course,
              with appropriate typedefs:

              typedef bool (*pred)(int,int );
              std::map<int, int, predmyMap(myPre dicate);
              >
              This becomes even more awkward if the key and data types of the map
              are something more complicated than int.
              Not at all. Again, typedefs.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              Working...