在遍历整个 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/