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