【发布时间】:2019-07-23 21:30:40
【问题描述】:
我有一个函数,我正在传递一个迭代器和一个字符串来检查迭代器是否指向第一个元素。 但是,我得到了意想不到的结果。
int main()
{
std::string str="abc";
std::string::iterator strit = str.begin();
iteratorProperty(strit, str);
}
void iteratorProperty(std::string::iterator it, std::string str) {
//std::next(it);
int count = 0;
for(auto i = it; i <str.end();i++) {
count++;
}
std::cout<<count<<std::endl;
}
这个 cout 语句返回值 51。 谁能帮我理解这一点?
谢谢。
【问题讨论】:
-
str是一个副本。您的开始和结束迭代器不兼容,它们属于不同的字符串。 -
谢谢。我修好了它。我太愚蠢了。这是一个愚蠢的问题。
-
@goodfellas95 不,这个问题是有效的。也许只是删除令人困惑的 main() 行,因为您既不打开也不关闭函数体。并告诉期望值;-)