<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Issue When Using [] Operator To Access Elements Of A map</title>
	<atom:link href="http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/feed/" rel="self" type="application/rss+xml" />
	<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/</link>
	<description>Sharing my ideas</description>
	<lastBuildDate>Sat, 26 Dec 2009 14:21:57 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Hawkeye Parker</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-134</link>
		<dc:creator>Hawkeye Parker</dc:creator>
		<pubDate>Wed, 13 May 2009 23:12:27 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-134</guid>
		<description>Thanks for the post.  I just fixed a legacy bug involving implicit add via [].  The code was not using find(), but was simply checking for values by [].  As a side affect, some unwanted string keys were added to the map, and those went out of scope while the static map (and the rest of its data) remained.  So, the next time we tried to access the map, it had a bunch of garbage in it, and this garbage was &quot;randomly&quot; spread around the map.  Hard to reproduce, and hard to fix, unless you&#039;re familiar with std::map.  I wouldn&#039;t have (and didn&#039;t) expect a get operation to modify the underlying map.

My take away isn&#039;t about how to initialize a std::map, but to never access one via [] without .find()&#039;ing first, especially if you&#039;re in static scope.  Freaky.</description>
		<content:encoded><![CDATA[<p>Thanks for the post.  I just fixed a legacy bug involving implicit add via [].  The code was not using find(), but was simply checking for values by [].  As a side affect, some unwanted string keys were added to the map, and those went out of scope while the static map (and the rest of its data) remained.  So, the next time we tried to access the map, it had a bunch of garbage in it, and this garbage was &#8220;randomly&#8221; spread around the map.  Hard to reproduce, and hard to fix, unless you&#8217;re familiar with std::map.  I wouldn&#8217;t have (and didn&#8217;t) expect a get operation to modify the underlying map.</p>
<p>My take away isn&#8217;t about how to initialize a std::map, but to never access one via [] without .find()&#8217;ing first, especially if you&#8217;re in static scope.  Freaky.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pizer</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-123</link>
		<dc:creator>pizer</dc:creator>
		<pubDate>Mon, 02 Mar 2009 14:00:26 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-123</guid>
		<description>It really depends on the implementation of the mapped_type.

There are situations where &quot;[]=&quot; might be faster than the insert. In the &quot;[]=&quot;-version mapped_type::operator= is used that might accept its parameter by value and use copy-and-swap. Together with a compiler employing RVO which eliminates this copy in case where the right hand side of the assignment was an rvalue you end up with an assignment that doesn&#039;t copy the object at all. It is constructed into a temporary and swapped with the default-constructed value of the map.

This should apply to many (well-designed) resource handle classes where copying is expensive and swapping is cheap.</description>
		<content:encoded><![CDATA[<p>It really depends on the implementation of the mapped_type.</p>
<p>There are situations where &#8220;[]=&#8221; might be faster than the insert. In the &#8220;[]=&#8221;-version mapped_type::operator= is used that might accept its parameter by value and use copy-and-swap. Together with a compiler employing RVO which eliminates this copy in case where the right hand side of the assignment was an rvalue you end up with an assignment that doesn&#8217;t copy the object at all. It is constructed into a temporary and swapped with the default-constructed value of the map.</p>
<p>This should apply to many (well-designed) resource handle classes where copying is expensive and swapping is cheap.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: spr</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-120</link>
		<dc:creator>spr</dc:creator>
		<pubDate>Fri, 27 Feb 2009 05:46:18 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-120</guid>
		<description>Using &quot;[]=&quot; operator to insert into a map creates a default value associated with key in the container, and then uses the assignment operatror to get the contents of val into the newly created
value. 
So, most probably insert operator should work faster than &quot;[]&quot;.</description>
		<content:encoded><![CDATA[<p>Using &#8220;[]=&#8221; operator to insert into a map creates a default value associated with key in the container, and then uses the assignment operatror to get the contents of val into the newly created<br />
value.<br />
So, most probably insert operator should work faster than &#8220;[]&#8220;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-115</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Tue, 24 Feb 2009 06:13:35 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-115</guid>
		<description>Dear alatiagFiny,
     thanks for your comment. 
     FYI, I know there is search engine and I&#039;ve been using many of them.
     Then, if you feel this post is a bullshit, you have to tell me why, rather than asking google to answer for you. I&#039;ve written what I&#039;ve experienced. 
     I hope you got my point.</description>
		<content:encoded><![CDATA[<p>Dear alatiagFiny,<br />
     thanks for your comment.<br />
     FYI, I know there is search engine and I&#8217;ve been using many of them.<br />
     Then, if you feel this post is a bullshit, you have to tell me why, rather than asking google to answer for you. I&#8217;ve written what I&#8217;ve experienced.<br />
     I hope you got my point.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alatiagFiny</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-112</link>
		<dc:creator>alatiagFiny</dc:creator>
		<pubDate>Sat, 21 Feb 2009 22:42:17 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-112</guid>
		<description>Maaaan, you know there is such thing in the web like search engine, http://google.com if you don&#039;t, go there to understand why this post is bullshit</description>
		<content:encoded><![CDATA[<p>Maaaan, you know there is such thing in the web like search engine, <a href="http://google.com" rel="nofollow">http://google.com</a> if you don&#8217;t, go there to understand why this post is bullshit</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-103</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Mon, 02 Feb 2009 12:05:31 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-103</guid>
		<description>&lt;strong&gt;[] operator&lt;/strong&gt; does not provide any facility to find if an element corresponding to a key exists or not. For example,
[sourcecode language=&#039;cpp&#039;]
map&lt;int, int&gt; myMap;
myMap[10] = 20;
myMap[10] = 40;
[/sourcecode]

will leave a value &lt;strong&gt;40 &lt;/strong&gt;corresponding to the key &lt;strong&gt;10&lt;/strong&gt;. However, with &lt;strong&gt;insert()&lt;/strong&gt; function, we have the option to see if the key already exists or not. It returns a pair. The iterator points to the element with the key specified and the bool shows if the operation was successful or not (If the element already existed, the insertion won&#039;t take place and the return value will be false).

For example,

[sourcecode language=&#039;cpp&#039;]
typedef map&lt;int, int&gt;::iterator MyMapIterator;
typedef pair&lt;int, int&gt; MyPair;
// Add a pair (10, 20) to the map
myMap[10] = 20;
// Try to add the pair (10, 30) using insert
 pair P = myMap.insert(MyPair(10, 30));
[/sourcecode]

Now the value of &lt;strong&gt;P.second&lt;/strong&gt; wil be false as the insertion failed and P.first will point to the element (10, 20).

If we had simply used the &lt;strong&gt;operator []&lt;/strong&gt; for insertion, we would have ended up with an element (10, 30).

However, when there is the need of overwriting the existing elements or when you are absolutely sure that you are not going to overwrite any of the values, you can use the &lt;strong&gt;operator []&lt;/strong&gt;. It is easy to use and more readable.</description>
		<content:encoded><![CDATA[<p><strong>[] operator</strong> does not provide any facility to find if an element corresponding to a key exists or not. For example,</p>
<pre class="brush: cpp;">
map&lt;int, int&gt; myMap;
myMap[10] = 20;
myMap[10] = 40;
</pre>
<p>will leave a value <strong>40 </strong>corresponding to the key <strong>10</strong>. However, with <strong>insert()</strong> function, we have the option to see if the key already exists or not. It returns a pair. The iterator points to the element with the key specified and the bool shows if the operation was successful or not (If the element already existed, the insertion won&#8217;t take place and the return value will be false).</p>
<p>For example,</p>
<pre class="brush: cpp;">
typedef map&lt;int, int&gt;::iterator MyMapIterator;
typedef pair&lt;int, int&gt; MyPair;
// Add a pair (10, 20) to the map
myMap[10] = 20;
// Try to add the pair (10, 30) using insert
 pair P = myMap.insert(MyPair(10, 30));
</pre>
<p>Now the value of <strong>P.second</strong> wil be false as the insertion failed and P.first will point to the element (10, 20).</p>
<p>If we had simply used the <strong>operator []</strong> for insertion, we would have ended up with an element (10, 30).</p>
<p>However, when there is the need of overwriting the existing elements or when you are absolutely sure that you are not going to overwrite any of the values, you can use the <strong>operator []</strong>. It is easy to use and more readable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pizer</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-102</link>
		<dc:creator>pizer</dc:creator>
		<pubDate>Mon, 02 Feb 2009 11:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-102</guid>
		<description>make that
typedef std::map&lt;std::string,std::string&gt; map_t

I forgot that wordpress likes to swallow angle brackets. ;)

-P</description>
		<content:encoded><![CDATA[<p>make that<br />
typedef std::map&lt;std::string,std::string&gt; map_t</p>
<p>I forgot that wordpress likes to swallow angle brackets. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>-P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pizer</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-101</link>
		<dc:creator>pizer</dc:creator>
		<pubDate>Mon, 02 Feb 2009 11:33:52 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-101</guid>
		<description>Why would insert be more elegant? I find insert for maps annoying because you have to create a pair of the right type:

typedef std::map map_t;
phonebook.insert(map_t::value_type(&quot;Alf&quot;,&quot;555 8531&quot;));

Also, the [] approach might be faster in some cases because it might not involve much copying. Any decently designed &quot;value class&quot; uses the &quot;copy &amp; swap&quot;-idiom for assignment which is known to avoid unneccesary copying when the compiler performs return-value-optimization, see &lt;a href=&quot;http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap&quot; rel=&quot;nofollow&quot;&gt;here.&lt;/a&gt;

-P</description>
		<content:encoded><![CDATA[<p>Why would insert be more elegant? I find insert for maps annoying because you have to create a pair of the right type:</p>
<p>typedef std::map map_t;<br />
phonebook.insert(map_t::value_type(&#8220;Alf&#8221;,&#8221;555 8531&#8243;));</p>
<p>Also, the [] approach might be faster in some cases because it might not involve much copying. Any decently designed &#8220;value class&#8221; uses the &#8220;copy &amp; swap&#8221;-idiom for assignment which is known to avoid unneccesary copying when the compiler performs return-value-optimization, see <a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap" rel="nofollow">here.</a></p>
<p>-P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-100</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Mon, 02 Feb 2009 10:01:35 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-100</guid>
		<description>Thanks for the comment.
[], indeed, is convenient to insert into map. Still, for the same, isn&#039;t the insert function more elegant as the other one ([]) has to return a (non-const) reference?

Please keep watching the blog.</description>
		<content:encoded><![CDATA[<p>Thanks for the comment.<br />
[], indeed, is convenient to insert into map. Still, for the same, isn&#8217;t the insert function more elegant as the other one ([]) has to return a (non-const) reference?</p>
<p>Please keep watching the blog.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pizer</title>
		<link>http://cppkid.wordpress.com/2009/01/29/issue-when-using-operator-to-access-elements-of-a-map/#comment-97</link>
		<dc:creator>pizer</dc:creator>
		<pubDate>Sat, 31 Jan 2009 16:38:04 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=285#comment-97</guid>
		<description>Since operator[] is supposed to return a reference there is only one other choice of action in case the key doesn&#039;t exist: throwing an exception. But operator[] the way it is currently defined is useful, too. I would still use it for writing to the map. It&#039;s just convenient.

map&lt;string,string&gt; phonebook;
phonebook[&quot;Alf&quot;] = &quot;555 8531&quot;;</description>
		<content:encoded><![CDATA[<p>Since operator[] is supposed to return a reference there is only one other choice of action in case the key doesn&#8217;t exist: throwing an exception. But operator[] the way it is currently defined is useful, too. I would still use it for writing to the map. It&#8217;s just convenient.</p>
<p>map&lt;string,string&gt; phonebook;<br />
phonebook["Alf"] = &#8220;555 8531&#8243;;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
