【问题标题】:How to extract next integer in an arbitrary position in string?如何在字符串中的任意位置提取下一个整数?
【发布时间】:2015-05-12 13:49:25
【问题描述】:

我的代码如下,我正在开发一个简单的文本编辑器。用户需要能够输入以下格式:


//其中n是表示行号的任何整数。

我在下面使用了 switch 语句来查看他们输入的第一个字符是什么,但在 case 'I'(插入)和 case 'D'(删除)中我需要能够提取他们之后输入的整数。

例如:

D 16 // 删除第 16 行
I 9 // 在第 9 行插入字符串
L // 列出所有行

我尝试了一些不同的方法,但都没有顺利进行,所以我想知道是否有更好的方法来做到这一点。

void handle_choice(string &choice)
{   
    int line_number;

      // switch statement according to the first character in string choice.
    switch (choice[0]) 
    {

    case 'I':

       // code here to extract next integer in the string choice

      break;

    case 'D':

      break;

    case 'L':

      break;

    case 'Q':

      break;

    default:
      break;
    }

我尝试了一些不同的方法,例如 getline() 和 cin

谢谢。

【问题讨论】:

    标签: c++ string stream cin getline


    【解决方案1】:
    #include <cctype>
    #include <string>
    using namespace std;
    
    // This function takes the whole input string as input, and
    // returns the first integer within that string as a string.
    
    string first_integer(string input) {
       // The digits of the number will be added to the string
       // return_value. If no digits are found, return_value will
       // remain empty.
       string return_value;
       // This indicates that no digits have been found yet.
       // So long as no digits have been found, it's okay
       // if we run into non-digits.
       bool in_number = false;
    
       // This for statement iterates over the whole input string.
       // Within the for loop, *ix is the character from the string
       // currently being considered. 
       for(string::iterator ix = input.begin(); ix != input.end(); ix++) {
         // Check if the character is a digit.
         if(isdigit(*ix)) {
             // If it is, append it to the return_value. 
             return_value.push_back(*ix);
             in_number = true;
         } else if(in_number) {
             // If a digit has been found and then we find a non-digit
             // later, that's the end of the number.
             return return_value;
         }
       }
       // This is reached if there are no non-digit characters after
       // the number, or if there are no digits in the string. 
       return return_value;
    }
    

    在你的 switch 语句中,你可以这样使用它:

    case 'I':
         string number = first_integer(choice);
         // Convert the string to an int here.
    

    【讨论】:

    • 虽然我真的很感谢您的回答,但我充其量只是个新手,我很难了解您的代码中发生的事情以及如何实现它。
    • 我添加了一些 cmets 来解释它在做什么以及如何处理该函数。
    • 对这个功能的解释很透彻,我现在明白了。谢谢
    【解决方案2】:

    你不需要定义一个函数,我会这样做:

    char command; 
    int line; 
    cin >> command >> line; //put the first character in command, and the next one in line
    

    如果您确实想使用函数,则应将字符串转换为字符串流。字符串流让您可以像 cin 一样为变量赋值、处理所有转换、在输入失败时告诉您以及跳过空格。

    所以在你的函数中,你首先要创建一个字符串流

    stringstream inputStream (choice); //make a stringstream from the input string
    

    接下来,将字符串流的值输入到上面的变量中:

    char command; 
    int line; 
    inputStream >> command >> line;
    

    现在,命令包含字母,行包含数字。但是,有时没有行号,只有命令。在这种情况下,第二个输入将失败。方便的是,字符串流可以让您检查:

    inputStream >> command >> line; 
    if(inputStream.fail()) { //input to line failed, probably because there was no number
    /*your code for when there is no number*/
    }
    

    您可能需要添加额外的检查。

    【讨论】:

    • 虽然我选择了其他答案,但这也有效,我最终在代码的其他地方使用它,所以谢谢@DarvishKamalia
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多