问题与std :: string :: find() [英] Problem with std::string::find()

查看:66
本文介绍了问题与std :: string :: find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用std :: string :: find()时遇到了麻烦.我通过以下代码从控制台读取了字符串:

I'm in trouble using std::string::find(). I read strings from console through the following code:

 50   while(command.find(exitString) != 0) {
 51         std::cout << "$ ";
 52         getline(std::cin, command);
 53 
 54         doSwitch(command);
 55     }

然后我通过以下功能打开"它们:

and then I "switch" on them, through the following function:

 59 void Console::doSwitch(std::string command) {
 60     if(command.find(helpString) == 0) {
 61         help();
 62     } else if(command.find(loadString) == 0) {
 63         try {
 64             doLoad(command);
 65         } catch(std::string str) {
 66             std::cout << str << std::endl;
 67         }
 68     } else if(command.find(dumpProcString) == 0) {
 69         try {
 70             doDumpProc(command);
 71         } catch(std::string str) {
 72             std::cout << str << std::endl;
 73         }
 74     } else if(command.find(dumpMemString) == 0) {
 75         doDumpMem();
 76     } else if(command.find(defmemString) == 0) {
 77         try {
 78             doDefmem(command);
 79         } catch(std::string str) {
 80             std::cout << str << std::endl;
 81         } catch(char *str) {
 82             std::cout << str << std::endl;
 83         }
 84     } else if(command.find(resetString) == 0) {
 85         try {
 86             doReset();
 87         } catch(std::string str) {
 88             std::cout << str << std::endl;
 89         }
 90     } else {
 91         std::cout << "Comando inválido." << std::endl;
 92     }
 93 }

,但有时根本无法正确切换.有任何线索吗?

but sometimes it simply doesn't switch correctly. Any clues?

预先感谢

我做了一些测试,发现它落在最后的else-if语句上,而不是落在最后的else-if语句上.然后我再次检查我的代码,发现根本原因是我忘记初始化resetString.问题解决了!谢谢大家.

I've done some tests and I detected it was falling on the last else-if statement, instead of falling on the last else. Then I checked my code again and found that the root cause was that I forgot to initialize resetString. Problem solved! Thank you everyone.

推荐答案

您可能期望 find 在找到字符串时返回零,类似于 strcmp 有效.

You might be expecting that find returns zero when it found the string, kind of like the way strcmp works.

但这不是查找的工作方式. find 返回找到的字符串的第一个索引,该索引可能为零,或者如果要查找的字符串前面带有空格,其他字符串等,则可能为其他索引.

But that's not how find works. find returns the first index of the found string, which might be zero, or might be something else if the string you're looking for is prepended with spaces, other strings, etc.

如果找不到 find ,则返回 string :: npos .因此,您的if ... else块应该检查以发现是否找到了字符串,而不是检查它们是否在索引零处.像这样:

If find doesn't find what you're looking for, it returns string::npos. So your if...else block should be checking to find if the strings were found or not found, not checking to see if they were at index zero. Like this:

if(command.find(helpString) != string::npos ) {
          help();
      } else if /// ... etc...

这篇关于问题与std :: string :: find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆