std::string::find 总是返回 string::npos 偶数
std::string::find always returns string::npos even
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
我认为你应该使用 std::find_first_of 算法遍历输入。从这个返回是一个积分器,所以你的循环应该在这个等于 std::end(input( 时退出。使用此迭代器,您可以向前看以下字符是否是您的""。
这是未编译的代码,仅用于指示:
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//
}
}
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- 在先前调用 string::find 后添加内存分配和内存集会导致它返回 npos.为什么?
- std::string::find 总是返回 string::npos 偶数
- string::npos如何知道我指的是哪个字符串
- 不同大小的 std::string::npos
- std::string::erase() 的定义行为是什么,带有 string::npos
- 为什么我们有 std::string::npos 但没有 std::vector::npos
- C++ std::string::find 总是返回 npos
- 简单的问题:为什么字符串库函数std::string::find总是返回std::string::npos
- std::string::npos == -1是否总是为真