c++ map<string,string>::find seems to return garbage iterator
<p><code>map&lt;string,string&gt;::find</code> seems to be returning garbage iterator, since i can access neither <code>my_it-&gt;first</code> nor second (NB: <code>my_it != my_map.end()</code> is verified). VC2010 reports a debug error, and looking deeper reveals </p>

<pre><code>my_it is (Bad Ptr, Bad Ptr).
</code></pre>

<p>The 'offending' map is a class attribute, <code>_match</code>, shown below in context:</p>

<pre><code>class NicePCREMatch
{
private:
    map&lt;string, string, less&lt;string&gt; &gt; _match;

public:
    void addGroup(const string&amp; group_name, const string&amp; value);
    string group(const string&amp; group_name);
};
</code></pre>

<p>Here is the code that returns elements by key (the commented-out code works fine):</p>

<pre><code>string NicePCREMatch::group(const string&amp; group_name)
{
    /*for (map&lt;string, string, less&lt;string&gt; &gt;::iterator j = _match.begin(); j != _match.end(); j++)
    {
        if(!strcmp(j-&gt;first.c_str(), group_name.c_str()))
        {
            return j-&gt;second;
        }
    }

    throw runtime_error("runtime_error: no such group");*/

    map&lt;string, string, less&lt;string&gt; &gt;::iterator i = _match.find(group_name);

    if (i == _match.end())
    {
        throw runtime_error("runtime_error: no such group");
    }

    return i-&gt;second;
}
</code></pre>

<p>And Here is the code that inserts new elements in the map:</p>

<pre><code>void NicePCREMatch::addGroup(const string&amp; group_name, const string&amp; value)
{
    _match.insert(pair&lt;string, string&gt;(group_name, value));
}
</code></pre>

<p>Another class uses NicePCREMatch as follows:</p>

<pre><code>template&lt;class Match_t&gt;
vector&lt;Match_t&gt; NicePCRE&lt;Match_t&gt;::match(const string&amp; buf)
{
[snip]
    Match_t m;
[snip]
    m.addGroup(std::string((const char *)tabptr + 2, name_entry_size - 3), \
                buf.substr(ovector[2*n], ovector[2*n+1] - ovector[2*n]));
[snip]
    addMatch(m);
[snip]
    return _matches;
}
</code></pre>

<p>Where, </p>

<pre><code>template&lt;class Match_t&gt;
void NicePCRE&lt;Match_t&gt;::addMatch(const Match_t&amp; m) 
{ 
    _matches.push_back(m);
}
</code></pre>

<p>Finally, client code uses NicePCRE class as follows:</p>

<pre><code>void test_NicePCRE_email_match(void)
{
    NicePCRE&lt;&gt; npcre;
    npcre.compile("(?P&lt;username&gt;[a-zA-Z]+?)(?:%40|@)(?P&lt;domain&gt;[a-zA-Z]+\.[a-zA-Z]{2,6})");
    vector&lt;NicePCREMatch&gt; matches = npcre.match("toto@yahoo.com");
    assert(!matches.empty());
    assert(!strcmp(matches.begin()-&gt;group("username").c_str(), "toto"));
    cout &lt;&lt; matches.begin()-&gt;group("domain").c_str() &lt;&lt; endl;
    assert(!strcmp(matches.begin()-&gt;group("domain").c_str(), "yahoo.com"));
}
</code></pre>

<p>BTW, this --is pretty much-- my main (the oddest TDD ever :) ):</p>

<pre><code>int main()
{
    int test_cnt = 0;
    cout &lt;&lt; "Running test #" &lt;&lt; test_cnt &lt;&lt; " .." &lt;&lt; endl;
    test_NicePCRE_email_match();
    cout &lt;&lt; "OK." &lt;&lt; endl &lt;&lt; endl;
    test_cnt++;

    SleepEx(5000, 1);

    return 0;
}
</code></pre>

<p>What am I doing wrong here?</p>

<p><strong>EDIT:</strong>
The following modification (compare with the version above) solved my problem. Viz,</p>

<pre><code>void NicePCREMatch::addGroup(const string&amp; group_name, const string&amp; value)
{
    _match.insert(pair&lt;string, string&gt;(group_name.c_str(), value.c_str()));
}
</code></pre>

<p>Client code (slightly modified) now looks like this:</p>

<pre><code>void test_NicePCRE_email_match(void)
{
    NicePCRE&lt;&gt; npcre;
    npcre.compile("(?P&lt;username&gt;[a-zA-Z]+?)(?:%40|@)(?P&lt;domain&gt;[a-zA-Z]+\.[a-zA-Z]{2,6})");
    vector&lt;NicePCREMatch&gt; matches = npcre.match("toto@yahoo.com");
    assert(!matches.empty());
    try
    {
        assert(!strcmp(matches.begin()-&gt;group("username").c_str(), "toto"));
        assert(!strcmp(matches.begin()-&gt;group("domain").c_str(), "yahoo.com"));
        cout &lt;&lt; "username = " &lt;&lt; matches.begin()-&gt;group("username") &lt;&lt; endl;
        cout &lt;&lt; "domain = " &lt;&lt; matches.begin()-&gt;group("domain") &lt;&lt; endl;
    }
    catch (const runtime_error&amp; e)
    {
        cout &lt;&lt; "Caught: " &lt;&lt; e.what() &lt;&lt; endl;
        assert(0x0);
    }
}
</code></pre>

<p>This is quite bizarre. Can someone please explain. However, I consider my problem solved already.</p>

<p>Thanks every one. </p>
