Update a value in std::map?

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

    Update a value in std::map?

    If I have:

    std::map<int,in tm;

    where I have:

    m.insert(std::m ake_pair(1,2));

    how do I update the value to 7 associated with key = 1?



    I was thinking:

    (*m.find(1)).se cond = 7;

    But is there no simpler way?
  • Ondra Holub

    #2
    Re: Update a value in std::map?

    On 4 íj, 13:37, desktop <f...@sss.comwr ote:
    If I have:
    >
    std::map<int,in tm;
    >
    where I have:
    >
    m.insert(std::m ake_pair(1,2));
    >
    how do I update the value to 7 associated with key = 1?
    >
    I was thinking:
    >
    (*m.find(1)).se cond = 7;
    >
    But is there no simpler way?
    m[1] = 7;

    Comment

    • Ondra Holub

      #3
      Re: Update a value in std::map?

      On 4 íj, 13:38, Ondra Holub <ondra.ho...@po st.czwrote:
      On 4 íj, 13:37, desktop <f...@sss.comwr ote:
      >
      If I have:
      >
      std::map<int,in tm;
      >
      where I have:
      >
      m.insert(std::m ake_pair(1,2));
      >
      how do I update the value to 7 associated with key = 1?
      >
      I was thinking:
      >
      (*m.find(1)).se cond = 7;
      >
      But is there no simpler way?
      >
      m[1] = 7;
      Please note, operator[] has "side effect". In case, that key value is
      not present in map, new one is added. But for your usage it should be
      ok.

      Comment

      • Chris ( Val )

        #4
        Re: Update a value in std::map?

        On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
        If I have:
        >
        std::map<int,in tm;
        >
        where I have:
        >
        m.insert(std::m ake_pair(1,2));
        >
        how do I update the value to 7 associated with key = 1?
        >
        I was thinking:
        >
        (*m.find(1)).se cond = 7;
        >
        But is there no simpler way?
        Yes:

        m[1]=7;

        But check that you have a key with a value
        of 1 before doing that, because if it does
        not exist, it will be created.

        Use the map::find member to check it.

        --
        Chris Val


        Comment

        • desktop

          #5
          Re: Update a value in std::map?

          Chris ( Val ) wrote:
          On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
          >If I have:
          >>
          >std::map<int,i ntm;
          >>
          >where I have:
          >>
          >m.insert(std:: make_pair(1,2)) ;
          >>
          >how do I update the value to 7 associated with key = 1?
          >>
          >I was thinking:
          >>
          >(*m.find(1)).s econd = 7;
          >>
          >But is there no simpler way?
          >
          Yes:
          >
          m[1]=7;
          >
          But check that you have a key with a value
          of 1 before doing that, because if it does
          not exist, it will be created.
          >
          Use the map::find member to check it.
          >
          --
          Chris Val
          >
          >
          Ok if I just want to subtract 1 I can just do:

          m[1] = m[1]-1;

          Comment

          • Chris ( Val )

            #6
            Re: Update a value in std::map?

            On Oct 4, 11:38 pm, desktop <f...@sss.comwr ote:
            Chris ( Val ) wrote:
            >
            >
            >
            >
            >
            On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
            If I have:
            >
            std::map<int,in tm;
            >
            where I have:
            >
            m.insert(std::m ake_pair(1,2));
            >
            how do I update the value to 7 associated with key = 1?
            >
            I was thinking:
            >
            (*m.find(1)).se cond = 7;
            >
            But is there no simpler way?
            >
            Yes:
            >
            m[1]=7;
            >
            But check that you have a key with a value
            of 1 before doing that, because if it does
            not exist, it will be created.
            >
            Use the map::find member to check it.
            >
            --
            Chris Val
            >
            Ok if I just want to subtract 1 I can just do:
            >
            m[1] = m[1]-1;
            m[ 1 ] -= 1;

            --
            Chris Val

            Comment

            • desktop

              #7
              Re: Update a value in std::map?

              Chris ( Val ) wrote:
              On Oct 4, 11:38 pm, desktop <f...@sss.comwr ote:
              >Chris ( Val ) wrote:
              >>
              >>
              >>
              >>
              >>
              >>On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
              >>>If I have:
              >>>std::map<int ,intm;
              >>>where I have:
              >>>m.insert(std ::make_pair(1,2 ));
              >>>how do I update the value to 7 associated with key = 1?
              >>>I was thinking:
              >>>(*m.find(1)) .second = 7;
              >>>But is there no simpler way?
              >>Yes:
              >>m[1]=7;
              >>But check that you have a key with a value
              >>of 1 before doing that, because if it does
              >>not exist, it will be created.
              >>Use the map::find member to check it.
              >>--
              >>Chris Val
              >Ok if I just want to subtract 1 I can just do:
              >>
              >m[1] = m[1]-1;
              >
              m[ 1 ] -= 1;
              >
              --
              Chris Val
              >
              Ok but my suggestion also works right - currently not possible to check it.

              Comment

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

                #8
                Re: Update a value in std::map?

                On 2007-10-04 19:13, desktop wrote:
                Chris ( Val ) wrote:
                >On Oct 4, 11:38 pm, desktop <f...@sss.comwr ote:
                >>Chris ( Val ) wrote:
                >>>
                >>>
                >>>
                >>>
                >>>
                >>>On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
                >>>>If I have:
                >>>>std::map<in t,intm;
                >>>>where I have:
                >>>>m.insert(st d::make_pair(1, 2));
                >>>>how do I update the value to 7 associated with key = 1?
                >>>>I was thinking:
                >>>>(*m.find(1) ).second = 7;
                >>>>But is there no simpler way?
                >>>Yes:
                >>>m[1]=7;
                >>>But check that you have a key with a value
                >>>of 1 before doing that, because if it does
                >>>not exist, it will be created.
                >>>Use the map::find member to check it.
                >>>--
                >>>Chris Val
                >>Ok if I just want to subtract 1 I can just do:
                >>>
                >>m[1] = m[1]-1;
                >>
                >m[ 1 ] -= 1;
                >>
                >--
                >Chris Val
                >>
                >
                Ok but my suggestion also works right - currently not possible to check it.
                Yes, or you can do just '--m[1];'.

                --
                Erik Wikström

                Comment

                • Mark P

                  #9
                  Re: Update a value in std::map?

                  desktop wrote:
                  Chris ( Val ) wrote:
                  >On Oct 4, 11:38 pm, desktop <f...@sss.comwr ote:
                  >>Chris ( Val ) wrote:
                  >>>
                  >>>
                  >>>
                  >>>
                  >>>
                  >>>On Oct 4, 9:37 pm, desktop <f...@sss.comwr ote:
                  >>>>If I have:
                  >>>>std::map<in t,intm;
                  >>>>where I have:
                  >>>>m.insert(st d::make_pair(1, 2));
                  >>>>how do I update the value to 7 associated with key = 1?
                  >>>>I was thinking:
                  >>>>(*m.find(1) ).second = 7;
                  >>>>But is there no simpler way?
                  >>>Yes:
                  >>>m[1]=7;
                  >>>But check that you have a key with a value
                  >>>of 1 before doing that, because if it does
                  >>>not exist, it will be created.
                  >>>Use the map::find member to check it.
                  >>>--
                  >>>Chris Val
                  >>Ok if I just want to subtract 1 I can just do:
                  >>>
                  >>m[1] = m[1]-1;
                  >>
                  >m[ 1 ] -= 1;
                  >>
                  >--
                  >Chris Val
                  >>
                  >
                  Ok but my suggestion also works right - currently not possible to check it.
                  It works, but it requires two lookups instead of one.

                  Comment

                  • Juha Nieminen

                    #10
                    Re: Update a value in std::map?

                    Mark P wrote:
                    It works, but it requires two lookups instead of one.
                    I'm wondering if any compiler is smart enough to optimize the second
                    lookup away.

                    Comment

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

                      #11
                      Re: Update a value in std::map?

                      On 2007-10-04 20:37, Juha Nieminen wrote:
                      Mark P wrote:
                      >It works, but it requires two lookups instead of one.
                      >
                      I'm wondering if any compiler is smart enough to optimize the second
                      lookup away.
                      It would be hard, since there is not guarantee (from the language) that
                      the reference returned the second time will be the same as on the first
                      call. A human can quite easily see that it will be, but I am not sure if
                      the compiler can.

                      --
                      Erik Wikström

                      Comment

                      Working...