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

时间:2017-06-11 19:48:41

标签: c++ string stdstring

std :: string :: find总是返回string :: npos,即使它应该找到一些东西。在这种情况下,我试图找到一个{后跟一个新行。但无论我放在哪里,它都找不到它。

pos=0;
while(pos!=string::npos)
{
    ko=input.find("{\n"); //here is the problem!!!
    if(ko!=string::npos && input[ko]=='\n')
    {
        input.erase(ko, 3);
        kc=input.find("}", ko);
        for(pos=input.find("\",", ko); pos<kc; pos=input.find("\",\n", pos))
        {
            input.erase(pos, 4);
            input.insert(pos, " | ");
        }
        pos=input.find("\"\n", ko);
        input.erase(pos, 3);
    }
    else
    {
        break;
    }
}
pos=0;
cn=1;
for(pos=input.find("\"", pos); pos!=string::npos; pos=input.find("\"", pos))
{
    input.erase(pos,1);
    if(cn)
    {
        input.insert(pos,"R");
    }
    cn=1-cn;
}

这里有一条输入:

-- declaring identifiers of state and final states
Detecting_SIDS = {
    "Detecting",
    "Detecting_CleanAir",
    "Detecting_GasPresent"
}

-- declaring identifiers of transitions
Detecting_TIDS = {
    "__NULLTRANSITION__",
    "Detecting_t2",
    "Detecting_t3",
    "Detecting_t4",
    "Detecting_t5"
}

此代码应该将上面的输入转换为以下内容:

-- declaring identifiers of state and final states
datatype Detecting_SIDS = RDetecting | RDetecting_CleanAir | RDetecting_GasPresent

-- declaring identifiers of transitions
datatype Detecting_TIDS = RNT | RDetecting_t2 | RDetecting_t3 | RDetecting_t4 | RDetecting_t5

1 个答案:

答案 0 :(得分:0)

我认为您应该使用std::find_first_of算法遍历输入。从这返回的是一个积分器,所以你的循环应该退出,这等于std :: end(输入)。使用此迭代器,您可以向前看以查看后面的字符是否为'\ n'。

这是仅用于指示的未编译代码:

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//
    }
}
相关问题