C++ 在字符串中查找特殊部分
我试图在字符串中找到一个特殊的部分。 字符串的示例如下所示:C++ 在字符串中查找特殊部分,c++,string,C++,String,我试图在字符串中找到一个特殊的部分。 字符串的示例如下所示: 22.21594087,1.688530832,0 我想找到1.688530832out。 我试过了 temp.substr(temp.find(“,”)+1,temp.rfind(“,”) 得到1.688530832,0。 我用find_last_of()替换了rfind(),但仍然得到了相同的结果 temp.substr(temp.find(“,”)+1,temp.find\u last\u of(“,”) 我知道这是一个简单的问
22.21594087,1.688530832,0
我想找到1.688530832
out。
我试过了
temp.substr(temp.find(“,”)+1,temp.rfind(“,”)代码>
得到1.688530832,0
。
我用find_last_of()替换了rfind(),但仍然得到了相同的结果
temp.substr(temp.find(“,”)+1,temp.find\u last\u of(“,”)代码>
我知道这是一个简单的问题,还有其他的解决方案。但我只想知道为什么rfind不起作用。
多谢各位 substr的第二个参数不是结束索引,而是所需子字符串的长度。只要加上1.688530832
的长度就可以了
如果搜索字符串的长度不可用,则可以找到最后一个逗号的位置,并从特殊单词的第一个字符的位置减去该位置:
auto beginning_index = temp.find(",") + 1;
auto last_comma_index = temp.rfind(",");
temp.substr(beginning_index, last_comma_index - beginning_index);
我知道你在做什么。您试图在子字符串的开头和结尾使用某种迭代器。不幸的是,substr
不是这样工作的,而是需要一个索引和该索引的偏移量来选择子字符串
您试图实现的目标可以通过std::find
完成,它可以与迭代器一起工作:
auto a = std::next(std::find(begin(temp), end(temp), ','));
auto b = std::next(std::find(rbegin(temp), rend(temp), ',')).base();
std::cout << std::string(a, b);
auto a=std::next(std::find(开始(临时),结束(临时),',');
auto b=std::next(std::find(rbegin(temp),rend(temp),',')).base();
标准::cout