【问题标题】:Check if the following position of a pointer is null or end of line C++检查指针的以下位置是否为空或行尾 C++
【发布时间】:2015-05-31 22:32:27
【问题描述】:

好吧,我在 C++ 中有一个字符串迭代器

std::string::iterator itB = itC->begin();

我想检查迭代器的下一个位置是否到达了行尾。我试过这个:

if(*itB ++ != NULL)

还有这个:

itB++ == itC->end()

但现在我真的很困惑,我需要一些帮助,因为我是 C++ 指针方面的新手。

【问题讨论】:

    标签: c++ string pointers null


    【解决方案1】:

    你想检查它而不修改它。您的两次尝试都涉及修改itB。如果你有 C++11,那就是 std::next:

    std::next(itB) == itC->end()
    

    如果你不这样做,就再做一个迭代器:

    std::string::iterator next = itB;
    ++next;
    next == itC->end()
    

    虽然,在这种情况下,我们知道 std::string::iterator 是一个随机访问迭代器,所以我们也可以这样做:

    itB + 1 == itC->end()
    

    (前两个适用于任何迭代器类别)

    【讨论】:

    • 情节扭曲:itC 是一个字符串列表的迭代器,抱歉我没有说清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2020-04-05
    相关资源
    最近更新 更多