Jump to content

std::map Memory Issue


Josh
 Share

Recommended Posts

It is stupid, it shouldn't add a value in when you're reading from it. They only did this because it has to return a pointer that can be gotten or set:

a = map[2]
map[2] = a

So it's a result of a limitation of the C++ language, because none of this stuff was implemented when they originally designed the thing. Which is why we call C++ an octopus made by nailing extra legs to a dog.

 

It is not stupid, and clearly defined in the standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf (23.4.4.3 map element access)

 

 

T& operator[](key_type&& x);

* Effects: If there is no key equivalent to x in the map, inserts value_type(std::move(x), T()) into

the map.

* Requires: mapped_type shall be DefaultConstructible.

* Returns: A reference to the mapped_type corresponding to x in *this.

* Complexity: logarithmic.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...