C++11 如何在字符串中查找字符,然后在字符与特定字符匹配时将其删除?
/*我必须找到角色并移除它们,基于它必须进入if/else if条件的角色。我在进入else if状态时面临困难*/C++11 如何在字符串中查找字符,然后在字符与特定字符匹配时将其删除?,c++11,C++11,/*我必须找到角色并移除它们,基于它必须进入if/else if条件的角色。我在进入else if状态时面临困难*/ #include <iostream> #include <boost/algorithm/string.hpp> #include <string> using namespace std; int main() { int fut = 0, spd =0; std::string symbol = "PGSh/d TWOG
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <string>
using namespace std;
int main() {
int fut = 0, spd =0;
std::string symbol = "PGSh/d TWOGK h/d"; //it will contain either 'h/d' or '/'
std::string str = "h/d";
std::string str1 = "/";
if(symbol.find(str)) //if it finds "h/d" then it belongs to future
{
++fut; //even one count is enough
boost::erase_all(symbol, "h/d");
std::cout<<"Future Instrument "<<std::endl;
}
else if(symbol.find(str1)) //if it finds "/" then it belongs to spread
{
++spd; //even one count is enough
boost::erase_all(symbol, "//");
std::cout<<"Spread Instrument "<<std::endl;
}
boost::erase_all(symbol, " ");
boost::to_upper(symbol);
std::cout<<symbol<<std::endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
int fut=0,spd=0;
std::string symbol=“PGSh/d TWOGK h/d”;//它将包含“h/d”或“/”
std::string str=“h/d”;
std::string str1=“/”;
if(symbol.find(str))//如果它找到了“h/d”,那么它属于未来
{
++fut;//即使是一次计数也足够了
boost::全部擦除(符号“h/d”);
std::coutw你说的“我在进入else-if条件时面临困难”是什么意思?你的第一个if
将是真的,因此根据定义,你永远不会接受else-if
分支。是的,但是如果符号有“PGS/TWOGK/”怎么办然后它也会进入分支内部,我想知道,是否有任何方法可以匹配整个str/str1而不仅仅是它的一部分。我仍然不确定你到底想要什么,但我想你想要的是什么?类似的东西会匹配“PGSx/y”
“PGSx/”
或“PGS/”
。请进一步说明您希望查找的模式以及您希望拒绝的模式。我想,如果符号中有“h/d”,则应转到if案例(未来仪器),如果符号中有“/”,则应转到else if案例。这在上述代码中没有发生。symbol.find(str)
返回找到子字符串的索引,如果未找到,则返回string::npos
。除非在索引0处找到子字符串,否则您的if
条件将为true。您可能需要if(symbol.find(str)!=std::string::npos)