且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

std::string::find 总是返回 string::npos 甚至

更新时间:2023-02-26 20:12:11

我认为您应该使用 std::find_first_of 算法.从 this 返回的是一个积分器,所以你的循环应该在 this 等于 std::end(input) 时退出.使用此迭代器,您可以提前查看以下字符是否是您的 '\n'.

I think you should traverse the input with the std::find_first_of algorithm. The return from this is an integrator so your loop should exit on this being equal to std::end(input). Using this iterator you can look ahead to see if the following character is your '\n'.

这是未编译的代码,仅供参考:

This is uncompiled code for indication only:

auto ptr = std::begin(input);
auto end = std::end(input);

while(ptr != end) 
{
    ptr = std::find_first_of(ptr, end, '{' );

    if (ptr == end)
        break;

    else if (++ptr == end)
        break;

    else if (*ptr == '\n)
    {
        //Do Processing//
    }
}