下面的代码经历了很多次循环,我很困惑为什么。我环顾四周,并没有看到完全像这样的情况。我对迭代器很新,所以这里可能有些简单。感谢您的帮助!希望对此的回应可以帮助其他人。
std::multimap<std::string,std::vector<Token> >::iterator end = theFacts.returnContents().end();
for (mapITER = theFacts.returnContents().begin() ; mapITER != end; mapITER++) {
cout << "ANOTHER ITERATION THROUGH FACTS" << endl;
cout << mapITER->first << endl;
cout << contents.begin()->first << endl;
if (mapITER->first == contents.begin()->first) {
cout << "same scheem type so I keep going!" << endl;
bool successfull = true;
cout << "starting to seek match --> size --> " << mapITER->second.size() << endl;
for (int x = 0; x< mapITER->second.size(); x++) {
std::cout << "processing! "
<< mapITER->second[x].getTokensValue() << "<<<<<<is equal?>>>>>>"
<< contents.begin()->second[x].getTokensValue() << std::endl;
if (mapITER->second[x].getTokensValue()
== contents.begin()->second[x].getTokensValue()) {
cout << "pushing value" << endl;
newBaby.push_back(contents.begin()->second[x]);
} else {
cout << "failure" << endl;
successfull = false;
}
}
if (successfull) {
std::cout << "match successfully found" << std::endl;
if (returnme.contents.empty()) {
returnme = Relation(contents.begin()->first, newBaby);
cout << returnme.toString() << endl;
} else {
returnme.relationInsert(contents.begin()->first, newBaby);
cout << returnme.toString() << endl;
}
} else {
// Anser is NO
}
}
}
我知道我没有提供完整的代码,但是你可以看到形成以下输出,我正在迭代的地图的大小是2,所以为什么它第三次循环!
WHERE TO END --> size of maps (number of iterations that shoudl occure2
ANOTHER ITERATION THROUGH FACTS
snap
snap
same scheem type so I keep going!
starting to seek match --> size --> 4
processing! '12345'<<<<<<is equal?>>>>>>'67890'
failure
processing! 'Snoopy'<<<<<<is equal?>>>>>>'Van Pelt'
failure
processing! '12 Apple'<<<<<<is equal?>>>>>>'34 Pear'
failure
processing! '555-1234'<<<<<<is equal?>>>>>>'555-5678'
failure
ANOTHER ITERATION THROUGH FACTS
snap
snap
same scheem type so I keep going!
starting to seek match --> size --> 4
processing! '67890'<<<<<<is equal?>>>>>>'67890'
pushing value
processing! 'Van Pelt'<<<<<<is equal?>>>>>>'Van Pelt'
pushing value
processing! '34 Pear'<<<<<<is equal?>>>>>>'34 Pear'
pushing value
processing! '555-5678'<<<<<<is equal?>>>>>>'555-5678'
pushing value
match successfully found
PRINT RELATION CALLED
snap('67890','Van Pelt','34 Pear','555-5678')
ANOTHER ITERATION THROUGH FACTS
Segmentation fault (core dumped)
这是返回内容的作用。
std::multimap<std::string,std::vector<Token> > Relation :: returnContents()
{
return contents;
}
其中contents是Relation类中的私有变量。在我看来它不应该导致错误,除非有一些我不知道的明显事物。
答案 0 :(得分:1)
这是返回内容的作用。
你有错误。函数returnContents
返回地图的副本。然后,您可以在其中的两个不同副本上调用begin
和end
。
返回(const)引用:
const std::multimap<std::string,std::vector<Token> >& Relation::returnContents() {
return contents;
}
或创建本地副本:
std::multimap<std::string,std::vector<Token> > tmp = theFacts.returnContents();
for (mapITER = tmp.begin() ; mapITER != tmp.end(); mapITER++) { ...