检查迭代仪上的第一个元素

Check Iterator Poinitng to first element of a string

本文关键字:第一个 元素 迭代 检查      更新时间:2023-10-16

我有一个函数,我正在传递一个迭代器和一个字符串,以检查迭代器是否指向第一个元素。但是,我得到了意外的结果。

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个值。有人可以帮我理解吗?

谢谢。

我传递了浅副本而不是深副本。

这就是为什么我获得不确定的值。

正确签名:

void iteratorProperty(std::string::iterator it, std::string& str) {