getting values in a map - map wrapper class

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

    getting values in a map - map wrapper class

    I am writing a class as a wrapper around a std::map.

    The class is:
    template<class TKey, class TValue>
    class CGeneralMap : protected map<TKey, TValue>

    At the moment I have basic functions like Add, Remove etc but I am
    really struggling with GetValue, which will be like the value obtained
    from [key]

    Eg if I create a:
    CGeneralMap<int , std::stringTest Map;

    and I have a key 3 with std::string value of John. then how in my
    GetValue function do I extract the "John"?


    In my GetValue function I tried:

    value = [3];

    but that doesn't work.

  • Chris ( Val )

    #2
    Re: getting values in a map - map wrapper class

    On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
    I am writing a class as a wrapper around a std::map.
    >
    The class is:
    template<class TKey, class TValue>
    class CGeneralMap : protected map<TKey, TValue>
    >
    At the moment I have basic functions like Add, Remove etc but I am
    really struggling with GetValue, which will be like the value obtained
    from [key]
    >
    Eg if I create a:
    CGeneralMap<int , std::stringTest Map;
    >
    and I have a key 3 with std::string value of John. then how in my
    GetValue function do I extract the "John"?
    >
    In my GetValue function I tried:
    >
    value = [3];
    >
    but that doesn't work.

    std::string getKeyValue( int index ) const
    {
    // Code...

    return TestMap[ index ];
    }

    --
    Chris Val

    Comment

    • Angus

      #3
      Re: getting values in a map - map wrapper class

      On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:
      On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
      >
      >
      >
      >
      >
      I am writing a class as a wrapper around a std::map.
      >
      The class is:
      template<class TKey, class TValue>
      class CGeneralMap : protected map<TKey, TValue>
      >
      At the moment I have basic functions like Add, Remove etc but I am
      really struggling with GetValue, which will be like the value obtained
      from [key]
      >
      Eg if I create a:
      CGeneralMap<int , std::stringTest Map;
      >
      and I have a key 3 with std::string value of John. then how in my
      GetValue function do I extract the "John"?
      >
      In my GetValue function I tried:
      >
      value = [3];
      >
      but that doesn't work.
      >
      std::string getKeyValue( int index ) const
      {
      // Code...
      >
      return TestMap[ index ];
      }
      >
      --
      Chris Val- Hide quoted text -
      >
      - Show quoted text -
      Yes of course I understand that. But I am talking about from within
      my class. From within my GetValue function.


      Comment

      • Chris ( Val )

        #4
        Re: getting values in a map - map wrapper class

        On Oct 2, 1:04 am, Angus <anguscom...@gm ail.comwrote:
        On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:
        >
        >
        >
        >
        >
        On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
        >
        I am writing a class as a wrapper around a std::map.
        >
        The class is:
        template<class TKey, class TValue>
        class CGeneralMap : protected map<TKey, TValue>
        >
        At the moment I have basic functions like Add, Remove etc but I am
        really struggling with GetValue, which will be like the value obtained
        from [key]
        >
        Eg if I create a:
        CGeneralMap<int , std::stringTest Map;
        >
        and I have a key 3 with std::string value of John. then how in my
        GetValue function do I extract the "John"?
        >
        In my GetValue function I tried:
        >
        value = [3];
        >
        but that doesn't work.
        >
        std::string getKeyValue( int index ) const
        {
        // Code...
        >
        return TestMap[ index ];
        }
        >
        --
        Chris Val- Hide quoted text -
        >
        - Show quoted text -
        >
        Yes of course I understand that. But I am talking about from within
        my class. From within my GetValue function.- Hide quoted text -
        What am I missing?
        What I showed you can be a member function if you put it in a class.

        --
        Chris Val



        Comment

        • Daniel Kraft

          #5
          Re: getting values in a map - map wrapper class

          Angus wrote:
          I am writing a class as a wrapper around a std::map.
          >
          The class is:
          template<class TKey, class TValue>
          class CGeneralMap : protected map<TKey, TValue>
          >
          At the moment I have basic functions like Add, Remove etc but I am
          really struggling with GetValue, which will be like the value obtained
          from [key]
          >
          Eg if I create a:
          CGeneralMap<int , std::stringTest Map;
          >
          and I have a key 3 with std::string value of John. then how in my
          GetValue function do I extract the "John"?
          >
          >
          In my GetValue function I tried:
          >
          value = [3];
          >
          but that doesn't work.
          You could of course use
          value = (*this)[3];

          But I think the "optimal" solution will be
          value = operator[](3);

          Both of those call the "operator[]" of the std::map, which is what you want.

          Cheers,
          Daniel


          --
          Got two Dear-Daniel-Instant Messages
          by MSN, associate ICQ with stress--so
          please use good, old E-MAIL!

          Comment

          • yanlinlin82@gmail.com

            #6
            Re: getting values in a map - map wrapper class

            "Chris ( Val )" <chris...@gmail .comwrote:
            std::string getKeyValue( int index ) const
            {
            // Code...
            >
            return TestMap[ index ];
            Can std::map<operat or [](size_t) be used in a const member function?
            }
            >

            Comment

            • Angus

              #7
              Re: getting values in a map - map wrapper class

              On 1 Oct, 18:32, Daniel Kraft <d...@domob.euw rote:
              Angus wrote:
              I am writing a class as a wrapper around a std::map.
              >
              The class is:
              template<class TKey, class TValue>
              class CGeneralMap : protected map<TKey, TValue>
              >
              At the moment I have basic functions like Add, Remove etc but I am
              really struggling with GetValue, which will be like the value obtained
              from [key]
              >
              Eg if I create a:
              CGeneralMap<int , std::stringTest Map;
              >
              and I have a key 3 with std::string value of John. then how in my
              GetValue function do I extract the "John"?
              >
              In my GetValue function I tried:
              >
              value = [3];
              >
              but that doesn't work.
              >
              You could of course use
              value = (*this)[3];
              >
              But I think the "optimal" solution will be
              value = operator[](3);
              >
              Both of those call the "operator[]" of the std::map, which is what you want.
              >
              Cheers,
              Daniel
              >
              --
              Got two Dear-Daniel-Instant Messages
              by MSN, associate ICQ with stress--so
              please use good, old E-MAIL!- Hide quoted text -
              >
              - Show quoted text -
              Ah brilliant, that is what I needed. But now I have a conversion
              problem.

              If on line 366 I have:
              Value = operator[](Key);

              and on line 367 I have:
              Value = (*this)[Key];

              Then I get these compilation errors in Microsoft Visual C++ version 6:
              TestBed.cpp


              generalmap.h(36 6) : error C2662: '[]' : cannot convert 'this' pointer
              from


              'const class CGeneralMap<int ,class std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char >'

              to

              'class std::map<int,cl ass std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char,struct
              std::less<int>, class std::allocator< class
              std::basic_stri ng<char,struct std::char_trait s<char>,class
              std::allocator< char &'

              Conversion loses qualifiers
              c:\program files\microsoft visual studio\vc98\inc lude
              \xmemory(59) : while compiling class-template member function 'void
              __thiscall CGeneralMap<int ,class std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char >::G
              etValue(const int,class std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char &) const'



              generalmap.h(36 7) : error C2678: binary '[' : no operator defined
              which takes a left-hand operand of type 'const class
              CGeneralMap<int ,class std::basic_stri ng<char,struct std::char_trait s<
              char>,class std::allocator< char >' (or there is no acceptable
              conversion)
              c:\program files\microsoft visual studio\vc98\inc lude
              \xmemory(59) : while compiling class-template member function 'void
              __thiscall CGeneralMap<int ,class std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char >::G
              etValue(const int,class std::basic_stri ng<char,struct
              std::char_trait s<char>,class std::allocator< char &) const'

              ie I am trying both options.

              Comment

              • Angus

                #8
                Re: getting values in a map - map wrapper class

                On 1 Oct, 18:32, Daniel Kraft <d...@domob.euw rote:
                Angus wrote:
                I am writing a class as a wrapper around a std::map.
                >
                The class is:
                template<class TKey, class TValue>
                class CGeneralMap : protected map<TKey, TValue>
                >
                At the moment I have basic functions like Add, Remove etc but I am
                really struggling with GetValue, which will be like the value obtained
                from [key]
                >
                Eg if I create a:
                CGeneralMap<int , std::stringTest Map;
                >
                and I have a key 3 with std::string value of John. then how in my
                GetValue function do I extract the "John"?
                >
                In my GetValue function I tried:
                >
                value = [3];
                >
                but that doesn't work.
                >
                You could of course use
                value = (*this)[3];
                >
                But I think the "optimal" solution will be
                value = operator[](3);
                >
                Both of those call the "operator[]" of the std::map, which is what you want.
                >
                Cheers,
                Daniel
                >
                --
                Got two Dear-Daniel-Instant Messages
                by MSN, associate ICQ with stress--so
                please use good, old E-MAIL!- Hide quoted text -
                >
                - Show quoted text -
                Its ok I had to fiddle with const qualifiers. It now works fine.

                Comment

                • Chris ( Val )

                  #9
                  Re: getting values in a map - map wrapper class

                  On Oct 2, 1:04 am, Angus <anguscom...@gm ail.comwrote:
                  On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:
                  >
                  >
                  >
                  >
                  >
                  On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
                  >
                  I am writing a class as a wrapper around a std::map.
                  >
                  The class is:
                  template<class TKey, class TValue>
                  class CGeneralMap : protected map<TKey, TValue>
                  >
                  At the moment I have basic functions like Add, Remove etc but I am
                  really struggling with GetValue, which will be like the value obtained
                  from [key]
                  >
                  Eg if I create a:
                  CGeneralMap<int , std::stringTest Map;
                  >
                  and I have a key 3 with std::string value of John. then how in my
                  GetValue function do I extract the "John"?
                  >
                  In my GetValue function I tried:
                  >
                  value = [3];
                  >
                  but that doesn't work.
                  >
                  std::string getKeyValue( int index ) const
                  {
                  // Code...
                  >
                  return TestMap[ index ];
                  }
                  >
                  Yes of course I understand that. But I am talking about from within
                  my class. From within my GetValue function.
                  In future, please try to describe what you're having difficulty
                  with a bit more clearly. You stated that you were having difficulty
                  with your member function "GetValue", not operator[] which is quite
                  different.

                  While you're at it, ensure that you check for the key's existence.

                  --
                  Chris Val







                  Comment

                  • Chris ( Val )

                    #10
                    Re: getting values in a map - map wrapper class

                    On Oct 2, 1:35 am, yanlinli...@gma il.com wrote:
                    "Chris ( Val )" <chris...@gmail .comwrote:
                    >
                    std::string getKeyValue( int index ) const
                    {
                    // Code...
                    >
                    return TestMap[ index ];
                    >
                    Can std::map<operat or [](size_t) be used in a const member function?
                    I don't think so.

                    --
                    Chris Val

                    Comment

                    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                      #11
                      Re: getting values in a map - map wrapper class

                      On 2007-10-01 16:28, Angus wrote:
                      I am writing a class as a wrapper around a std::map.
                      >
                      The class is:
                      template<class TKey, class TValue>
                      class CGeneralMap : protected map<TKey, TValue>
                      >
                      At the moment I have basic functions like Add, Remove etc but I am
                      really struggling with GetValue, which will be like the value obtained
                      from [key]
                      >
                      Eg if I create a:
                      CGeneralMap<int , std::stringTest Map;
                      >
                      and I have a key 3 with std::string value of John. then how in my
                      GetValue function do I extract the "John"?
                      >
                      >
                      In my GetValue function I tried:
                      >
                      value = [3];
                      >
                      but that doesn't work.

                      You should use the find() method for a GetValue method since there is no
                      guarantee that the key specified exists. Using the [] operator will
                      create an entry if it does not exist which is not what you want from a
                      GetValue method.

                      --
                      Erik Wikström

                      Comment

                      • Chris ( Val )

                        #12
                        Re: getting values in a map - map wrapper class

                        On Oct 2, 2:42 am, Erik Wikström <Erik-wikst...@telia. comwrote:
                        On 2007-10-01 16:28, Angus wrote:
                        [snip]
                        In my GetValue function I tried:
                        >
                        value = [3];
                        >
                        but that doesn't work.
                        >
                        You should use the find() method for a GetValue method since there is no
                        guarantee that the key specified exists. Using the [] operator will
                        create an entry if it does not exist which is not what you want from a
                        GetValue method.
                        That was exactly the thought I had, which led me to believe
                        the OP didn't want operator[].

                        --
                        Chris Val

                        Comment

                        • James Kanze

                          #13
                          Re: getting values in a map - map wrapper class

                          On Oct 1, 5:35 pm, yanlinli...@gma il.com wrote:
                          "Chris ( Val )" <chris...@gmail .comwrote:
                          std::string getKeyValue( int index ) const
                          {
                          // Code...
                          return TestMap[ index ];
                          Can std::map<operat or [](size_t) be used in a const member function?
                          }
                          Of course not.

                          The first problem is that he is deriving from std::map, when he
                          probably should be using containment; derivation is a very poor
                          way to "wrap" something. But having an std::map as a member
                          doesn't change the problem: you still can't use operator[] in a
                          const function.

                          The real question, of course, is what should happen if the value
                          isn't present in the map. Depending on what the map is used
                          for, several possibilities can be considered:

                          -- It's an error. The user should check beforehand (e.g. with
                          a contains() function which you provide). If the value
                          isn't present, it's an assertion failure, or possibly only
                          an exception.

                          -- It's an expected condition. The simplest solution here is
                          to return a pointer, returning NULL if the element isn't
                          present.

                          -- The element is by definition present, with a default value.
                          Basically, you implement something like the precedent, but
                          return "ptr == NULL ? defaultValue : *ptr".

                          -- The element is automatically inserted, with a default value.
                          This is the behavior of std::map, but it means that you
                          cannot read a const map.

                          The function behind whatever you do (except for the last) is
                          std::map<>::fin d. For the second solution (which is the most
                          general, and easiest to map into the other solutions), you do
                          something like:

                          ValueType const*
                          Map::get( KeyType const& key ) const
                          {
                          std::map<...>:: const_iterator
                          elem = myMap.find( key ) ;
                          return elem == myMap.end()
                          ? NULL
                          : &elem->second ;
                          }

                          --
                          James Kanze (GABI Software) email:james.kan ze@gmail.com
                          Conseils en informatique orientée objet/
                          Beratung in objektorientier ter Datenverarbeitu ng
                          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                          Comment

                          Working...