如何提取字符串中任意位置的下一个整数?
How to extract next integer in an arbitrary position in string?
我的代码在下面,我正在开发一个简单的文本编辑器。用户需要能够输入以下格式:
I n
//where n is any integer representing the line number.
我在下面使用了 switch 语句来查看他们输入的第一个字符是什么,但是在 case 'I'(插入)和 case 'D'(delete) 我需要能够提取他们之后输入的整数。
例如:
D 16 // deletes line 16
I 9 // Inserts string at line 9
L // lists all lines
我已经尝试了一些不同的方法,但没有一个是顺利的,所以我想知道是否有更好的方法来做到这一点。
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 <<
但如果用户没有以该特定格式输入该行,我将无法正常工作,我想知道是否有办法。
谢谢。
#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.
你不需要定义函数,我会这样做:
char command;
int line;
cin >> command >> line; //put the first character in command, and the next one in line
如果您确实想使用函数,则应将字符串转换为字符串流。 Stringstreams 让您可以像 cin 一样为变量赋值,处理所有转换,在输入失败时告诉您,并跳过空格。
所以在你的函数中,你首先要创建一个字符串流
stringstream inputStream (choice); //make a stringstream from the input string
接下来,将字符串流值输入到上述变量中:
char command;
int line;
inputStream >> command >> line;
现在,命令包含字母,行包含数字。但是,有时没有行号,只有命令。在这种情况下,第二个输入将失败。方便的是,stringstreams 让你检查这个:
inputStream >> command >> line;
if(inputStream.fail()) { //input to line failed, probably because there was no number
/*your code for when there is no number*/
}
您可能需要包括额外的检查。
我的代码在下面,我正在开发一个简单的文本编辑器。用户需要能够输入以下格式:
I n
//where n is any integer representing the line number.
我在下面使用了 switch 语句来查看他们输入的第一个字符是什么,但是在 case 'I'(插入)和 case 'D'(delete) 我需要能够提取他们之后输入的整数。
例如:
D 16 // deletes line 16
I 9 // Inserts string at line 9
L // lists all lines
我已经尝试了一些不同的方法,但没有一个是顺利的,所以我想知道是否有更好的方法来做到这一点。
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 << 但如果用户没有以该特定格式输入该行,我将无法正常工作,我想知道是否有办法。
谢谢。
#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.
你不需要定义函数,我会这样做:
char command;
int line;
cin >> command >> line; //put the first character in command, and the next one in line
如果您确实想使用函数,则应将字符串转换为字符串流。 Stringstreams 让您可以像 cin 一样为变量赋值,处理所有转换,在输入失败时告诉您,并跳过空格。
所以在你的函数中,你首先要创建一个字符串流
stringstream inputStream (choice); //make a stringstream from the input string
接下来,将字符串流值输入到上述变量中:
char command;
int line;
inputStream >> command >> line;
现在,命令包含字母,行包含数字。但是,有时没有行号,只有命令。在这种情况下,第二个输入将失败。方便的是,stringstreams 让你检查这个:
inputStream >> command >> line;
if(inputStream.fail()) { //input to line failed, probably because there was no number
/*your code for when there is no number*/
}
您可能需要包括额外的检查。