map<string,string>::find
似乎是返回垃圾迭代器,因为我既不能访问my_it->first
也不能访问第二个(注意:my_it != my_map.end()
已验证)。 VC2010报告调试错误,并寻求更深层次的揭示C++ map <string,string> :: find似乎返回垃圾迭代器
my_it is (Bad Ptr, Bad Ptr).
的“违规”的地图是一个类属性,_match
,上下文如下图所示:
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);
};
下面是一个由键返回元素的代码(被注释掉的代码工作正常):
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;
}
这里是插入在地图上的新元素的代码:
void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
_match.insert(pair<string, string>(group_name, value));
}
另一类使用NicePCREMatch如下:
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;
}
其中,
template<class Match_t>
void NicePCRE<Match_t>::addMatch(const Match_t& m)
{
_matches.push_back(m);
}
最后,客户端代码使用NicePCRE类,如下所示:
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("[email protected]");
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"));
}
顺便说一句,这一点 - - 非常 - 我的主要(有史以来最奇怪的TDD :)):
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;
}
我在做什么错在这里?
编辑: 下面的修改(与上述版本比较)解决了我的问题。即,
void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
_match.insert(pair<string, string>(group_name.c_str(), value.c_str()));
}
客户端代码(略有修改)现在看起来是这样的:
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("[email protected]");
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);
}
}
这是很奇怪的。有人可以请解释。不过,我认为我的问题已经解决了。
感谢每一位。
是否有可能'_match'被另一个线程同时修改?这种错误也可能是由于线程中其他地方的内存破坏导致的。 – 2012-02-22 10:46:01
感谢代理比约恩波利斯。不,只有一个线程才能访问_match映射。顺便说一句,如果它是由于其他线程(或一些其他线程不安全的东西)的内存腐败,我怎么解释注释掉的代码工作,但没有与地图::查找调用?)。 – dohmatob 2012-02-22 11:00:51
'map>'比较规范('less ''')需要什么? –
2012-02-22 11:01:11