CPP:string.find()== 0和string.find!= string :: npos]之差>

问题描述 投票:-1回答:2
Command* Function(const char* cmd_line) {

    string cmd = _trim(string(cmd_line));
    if(cmd.find(">") != string::npos)
    {
        return new something(cmd_line);
    }
  else if (cmd.find("pwd") == 0) {
    return new something1(cmd_line);
  }
  else if (cmd.find("chprompt") == 0){
      return new something2(cmd_line);
  }
...

为什么将第一个IF条件更改为cmd.find(“>”)== 0,我的程序无法工作。它仅适用于cmd.find!= npos。从逻辑上看,这两个If语句之间似乎没有区别,因此这实际上没有任何意义。

Command * Function(const char * cmd_line){string cmd = _trim(string(cmd_line)); if(cmd.find(“>”)!= string :: npos){返回新内容(cmd_line); }否则,如果(cmd.find(“ ...

c++
2个回答
1
投票

[find()返回找到子字符串的位置。


0
投票

字符串是数组,因此0为有效答案。例如(无真实代码):

© www.soinside.com 2019 - 2024. All rights reserved.