c++ - 我如何查看字符串迭代器的下一个值

标签 c++ string iterator

在遍历整个 string 的循环中,我如何查看迭代器的下一个值?

for (string::iterator it = inp.begin(); it!= inp.end(); ++it)
{
  // Just peek at the next value of it, without actually incrementing the iterator
}

这在 C 中非常简单,

for (i = 0; i < strlen(str); ++i) {
     if (str[i] == str[i+1]) {
         // Processing
     }
}

在 C++ 中有什么有效的方法可以做到以上几点?

注意:我没有使用 Boost。

最佳答案

if ( not imp.empty() )
{
    for (string::iterator it = inp.begin(); it!= inp.end(); ++it)
         if (it + 1 != inp.end() and *it == *(it + 1)) {
             // Processing
         }
    }
}

if ( not imp.empty() )
{
    for (string::iterator it = inp.begin(); it!= inp.end() - 1; ++it)
        if ( *it == *(it+1) ) {
            // Processing
        }
    }
}

关于c++ - 我如何查看字符串迭代器的下一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2025119/

相关文章:

c++ - const_iterator 和模板,编译错误

c++ - 在二维伊辛模型上工作。 C++ 新手。我在第 23 行收到错误 "expected unqualified-id before ' {' token"

c++ - 如何为不同的类设置 glfwSetKeyCallback?

c++ - 计算具有限制的唯一排列的有效方法

c - Scanf for string 无法正常工作

java - 想要统计Java中字符串的出现次数

c++ - 需要 libstdc++.so.6(GLIBCXX_3.4.21)(64bit)(Centos 7 错误)

java - 将类 Unicode 字符串转换为真正的 Unicode 字符串的最短代码

java - struts2 jSTL 迭代器标签

python - 在python中的封闭范围[a,b]中迭代