Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:58:40+00:00 2026-05-30T03:58:40+00:00

map<string,string>::find seems to be returning garbage iterator, since i can access neither my_it->first nor

  • 0

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

my_it is (Bad Ptr, Bad Ptr).

The ‘offending’ map is a class attribute, _match, shown below in context:

class NicePCREMatch
{
private:
    map<string, string, less<string> > _match;

public:
    void addGroup(const string& group_name, const string& value);
    string group(const string& group_name);
};

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

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

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

    map<string, string, less<string> >::iterator i = _match.find(group_name);

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

    return i->second;
}

And Here is the code that inserts new elements in the map:

void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
    _match.insert(pair<string, string>(group_name, value));
}

Another class uses NicePCREMatch as follows:

template<class Match_t>
vector<Match_t> NicePCRE<Match_t>::match(const string& 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;
}

Where,

template<class Match_t>
void NicePCRE<Match_t>::addMatch(const Match_t& m) 
{ 
    _matches.push_back(m);
}

Finally, client code uses NicePCRE class as follows:

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

BTW, this –is pretty much– my main (the oddest TDD ever 🙂 ):

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

    SleepEx(5000, 1);

    return 0;
}

What am I doing wrong here?

EDIT:
The following modification (compare with the version above) solved my problem. Viz,

void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
    _match.insert(pair<string, string>(group_name.c_str(), value.c_str()));
}

Client code (slightly modified) now looks like this:

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

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

Thanks every one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:58:42+00:00Added an answer on May 30, 2026 at 3:58 am

    Your issue is here

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

    Your find failed for some reason. I can’t say why because I don’t have the full code. But, after the failure, you are throwing an error, but there is nobody to catch outside. Please add a try catch at the point where you call the method group() and implement the logic if the match is not found.
    I tried with your sample snippets (+ some changes to get the stuff compiled) and it looks like visual studio continues with the next line in the function even after a throw statement. I don’t know the theory behind it. I was bit surprised at seeing such a behavior.

    [To make sure that your class structure is not causing the problem, I tried with a simple global method and even the method also gave me the same behavior. If there are somebody who can explain this please feel free.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a std::map: std::map<std::string, std::string> I'm passing string literal to find method. Obviously,
I am initializing a map map<string,int> ex; in C++. I could not find contains_key
std::map<std::string, int> m; // Can I make assumption that m[NoSuchKey] will return 0? std::cout
I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]],
my problem seems simple but I can not manage to find an answer here.
I have a large map of String->Integer and I want to find the highest
If i have a stl map from string to int and i want to
In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and
Heading Is there a way to map an arbitrary string to a HEX COLOR
What is the easiest way in Java to map strings (Java String ) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.