gpt4 book ai didi

c++ - 迭代器,循环导致段错误!为什么循环太多了?

转载 作者:行者123 更新时间:2023-11-28 06:56:37 41 4
gpt4 key购买 nike

下面的代码循环了太多次,我不明白为什么。我环顾四周,还没有看到完全像这样的案例。我对迭代器还很陌生,所以这里可能有些简单的东西。感谢您的帮助!并希望对此的回应在未来对其他人有所帮助。

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
}
}
}

我知道我没有提供完整的代码,但是正如您从下面的输出中看到的那样,我正在迭代的 map 的大小是 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 类中的私有(private)变量。在我看来它不会导致错误,除非有明显的我不知道的事情。

最佳答案

here is what return contents does.

这就是你的错误。 returnContents 函数返回 map 的拷贝。然后,您在它的两个不同拷贝上调用 beginend

要么返回一个(常量)引用:

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++) { ...

关于c++ - 迭代器,循环导致段错误!为什么循环太多了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23076797/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com