检查迭代仪上的第一个元素
Check Iterator Poinitng to first element of a string
我有一个函数,我正在传递一个迭代器和一个字符串,以检查迭代器是否指向第一个元素。但是,我得到了意外的结果。
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) {
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++ queue.front();为什么不从第一个元素开始呢?
- 指向数组基址的指针而不是指向第一个元素的指针有什么优点?
- 静态堆栈函数不会 1) 输入第一个元素 2)添加新元素时识别旧元素
- 从队列中删除第一个元素C++
- 如果第一个元素包含任何零,则我的程序以不希望的方式运行
- 为什么数组不打印第一个元素?
- 在数组中显示第一个元素用户输入
- 消除集合的第一个元素
- 为什么指向矢量第一个元素的指针会丢失?
- 如何打印出常量字符串的第一个元素?
- 读取数组的第一个元素还是第 4000 个元素更快?
- 如何在数组的一部分中查找特定值的第一个元素
- 对数组进行排序的算法,先是第一个元素,然后是前 2 个元素,然后是前 3 个元素,依此类推
- 访问指向对象指针向量的指针的第一个元素?
- 引用保留向量中的第一个元素
- 指向数组的指针是否应该等于指向其第一个元素的指针?
- 尝试将参数包的第一个元素作为函数调用,并将包的其余部分作为参数传递给它
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?