need for operator[] in map

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    need for operator[] in map

    For inserting new elements in map, we can use insert member function.

    To know if an element exists or not in a map, we can use count or find
    member function.

    Also, we can use the iterator returned by find to modify the mapped
    value of an existing key.

    When we use operator[], it may add an element into the map if the key
    already doesn't exist, which may not be always wanted.

    Given this, I am unable to understand the reason as to why we have
    operator[] in map whose functionality can be achieved by other member
    functions.

    Kindly clarify.

    Thanks
    V.Subramanian
  • [rob desbois]

    #2
    Re: need for operator[] in map

    On Feb 7, 2:56 pm, "subramanian10. ..@yahoo.com, India"
    <subramanian10. ..@yahoo.comwro te:
    For inserting new elements in map, we can use insert member function.
    >
    To know if an element exists or not in a map, we can use count or find
    member function.
    >
    Also, we can use the iterator returned by find to modify the mapped
    value of an existing key.
    >
    When we use operator[], it may add an element into the map if the key
    already doesn't exist, which may not be always wanted.
    >
    Given this, I am unable to understand the reason as to why we have
    operator[] in map whose functionality can be achieved by other member
    functions.
    >
    Kindly clarify.
    >
    Thanks
    V.Subramanian
    You've already got a response on your original post - if that doesn't
    answer the question, reply and give some information as to how it
    didn't help.
    Don't just post the same question again, especially on the same day.

    Rob.

    Comment

    • subramanian100in@yahoo.com, India

      #3
      Re: need for operator[] in map

      On Feb 7, 10:00 am, "[rob desbois]" <rob.desb...@gm ail.comwrote:
      On Feb 7, 2:56 pm, "subramanian10. ..@yahoo.com, India"
      >
      >
      >
      <subramanian10. ..@yahoo.comwro te:
      For inserting new elements in map, we can use insert member function.
      >
      To know if an element exists or not in a map, we can use count or find
      member function.
      >
      Also, we can use the iterator returned by find to modify the mapped
      value of an existing key.
      >
      When we use operator[], it may add an element into the map if the key
      already doesn't exist, which may not be always wanted.
      >
      Given this, I am unable to understand the reason as to why we have
      operator[] in map whose functionality can be achieved by other member
      functions.
      >
      Kindly clarify.
      >
      Thanks
      V.Subramanian
      >
      You've already got a response on your original post - if that doesn't
      answer the question, reply and give some information as to how it
      didn't help.
      Don't just post the same question again, especially on the same day.
      >
      Rob.
      If you are referring to the other thread with the subject
      "using operator[] or insert in map ?",
      then I would like to say that I got clarified for THAT doubt.

      If you are referring to some other thread, please give the subject of
      the thread you are referring to.

      In this particular thread ie the thread with the subject
      "need for operator[] in map",
      I want to know the reason for having operator[] in map.

      Since the modification of mapped-value for a given key can be done
      through dereferencing and modifying the iterator returned by find()
      operation, I - as a beginner in STL, am unable to understand the
      reason for having operator[] in map.

      I still think the question I have posted here is different from the
      other thread with the subject: "using operator[] or insert in map ?"

      Please provide me an answer for this thread.

      Thanks
      V.Subramanian

      Comment

      • tragomaskhalos

        #4
        Re: need for operator[] in map

        On Feb 7, 2:56 pm, "subramanian10. ..@yahoo.com, India"
        <subramanian10. ..@yahoo.comwro te:
        For inserting new elements in map, we can use insert member function.
        >
        To know if an element exists or not in a map, we can use count or find
        member function.
        >
        Also, we can use the iterator returned by find to modify the mapped
        value of an existing key.
        >
        When we use operator[], it may add an element into the map if the key
        already doesn't exist, which may not be always wanted.
        >
        Given this, I am unable to understand the reason as to why we have
        operator[] in map whose functionality can be achieved by other member
        functions.
        >
        My guess is: syntactic elegance, convenience,
        familiarity, symmetry. IOW it's nice to be
        able to be able to do:

        foomap["bar"] = "baz";
        // then
        s = foomap["bar"];

        You only need to look at code using [] vs
        the alternatives to see this for yourself.

        Comment

        • James Kanze

          #5
          Re: need for operator[] in map

          On Feb 7, 3:56 pm, "subramanian10. ..@yahoo.com, India"
          <subramanian10. ..@yahoo.comwro te:
          For inserting new elements in map, we can use insert member function.
          To know if an element exists or not in a map, we can use count or find
          member function.
          Also, we can use the iterator returned by find to modify the mapped
          value of an existing key.
          When we use operator[], it may add an element into the map if the key
          already doesn't exist, which may not be always wanted.
          Given this, I am unable to understand the reason as to why we have
          operator[] in map whose functionality can be achieved by other member
          functions.
          Convenience. It's a bit awkward, in that the semantics which
          would be most convenient vary according to what you're using the
          map for, and in practice, I rarely use []. (But then, most of
          my maps are const, so I can't.) The real question is, of
          course, if [] doesn't find the element, what does it do?

          In the most general case, because of such uncertainty surronding
          the semantics, one could argue that map shouldn't support an
          operator[] at all---that its a case of operator overloading
          abuse. In practice, however, smaller, text based languages like
          AWK and perl use it effectively, and it seems reasonable to
          support it in the same way they do---if you are using std::map
          like you'd use an array in AWK or perl, you use it (and not much
          else of the interface of std::map); if you are using std::map in
          some other context, you don't use it.

          --
          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

          • Daniel T.

            #6
            Re: need for operator[] in map

            <subramanian100 in@yahoo.com wrote:
            For inserting new elements in map, we can use insert member function.
            >
            To know if an element exists or not in a map, we can use count or find
            member function.
            >
            Also, we can use the iterator returned by find to modify the mapped
            value of an existing key.
            >
            When we use operator[], it may add an element into the map if the key
            already doesn't exist, which may not be always wanted.
            >
            Given this, I am unable to understand the reason as to why we have
            operator[] in map whose functionality can be achieved by other member
            functions.
            >
            Kindly clarify.
            For the answer, you will have to ask the standards body, or at least
            Alex Stepanov (the creator of the STL.)

            For now, maybe the explanation from SGI's website will do?

            Note that the definition of operator[] is extremely simple: m[k] is
            equivalent to (*((m.insert(va lue_type(k,
            data_type()))). first)).second. Strictly speaking, this member
            function is unnecessary: it exists only for convenience.
            (http://www.sgi.com/tech/stl/Map.html)

            Comment

            Working...