Key keeps getting replaced in dictionary C++ : <p>I'm trying to create a dictionary type of class for fun in C++, similar to the one seen in Python where you can designate a key and a value (which may be of any type in this case, including custom classes).</p>

<pre><code>for (unsigned int x = 0; x &lt; word.length(); x++) {
    if (!map.has_key(word[x])) {
        std::cout &lt;&lt; "CREATING " &lt;&lt; word[x] &lt;&lt; std::endl;
        map[word[x]] = ics::ArraySet&lt;char&gt;();
        map[word[x]].insert(word[x]);
    }
    for (int y = 0; y &lt; dist; y++) {
        std::cout &lt;&lt; "HELLO!" &lt;&lt; std::endl;
        if ((x + y) &lt; word.length())
            std::cout &lt;&lt; "ADDING " &lt;&lt; word[x+y] &lt;&lt; std::endl;
            map[word[x]].insert(word[(x + y)]);
        if ((x - y) &gt;= 0)
            map[word[x]].insert(word[(x - y)]);
    }
}
</code></pre>

<p>The issue that occurs is that my key keeps being replaced. I'm trying to find letters that are within "x" range of the current letter I'm on. I append these nearby keys into a set, which is the value of my dictionary in this scenario.</p>

<p>For an example: <strong>nearby(racecar,2)</strong></p>

<p>Should return a dictionary with values like this ...</p>

<p>dictionary('r' -> {r,a,c}, 'a' -> {r,c,e}, ...)</p>

<p>However, what happens is that the </p>

<pre><code>if (!map.has_key(word[x]))
</code></pre>

<p>keeps failing and my keys keep being recreated each and every time along with the sets.</p>
