从包含其他字符的字符串中提取尾随 int
Extract trailing int from string containing other characters
关于从 C++ 中的字符串中提取有符号整数,我遇到了问题。
假设我有一个字符串 images1234
,我如何在不知道 C++ 中最后一个非数字字符的位置的情况下从字符串中提取 1234
。
仅供参考,我已经尝试过 stringstream 以及其他人通过 post 建议的 lexical_cast 但是 stringstream returns 0 而 lexical_cast 停止工作。
int main()
{
string virtuallive("Images1234");
//stringstream output(virtuallive.c_str());
//int i = stoi(virtuallive);
//stringstream output(virtuallive);
int i;
i = boost::lexical_cast<int>(virtuallive.c_str());
//output >> i;
cout << i << endl;
return 0;
}
How can i extract the 1234 from the string without knowing the position of the last non numeric character in C++?
你不能。不过位置不难找:
auto last_non_numeric = input.find_last_not_of("1234567890");
char* endp = &input[0];
if (last_non_numeric != std::string::npos)
endp += last_non_numeric + 1;
if (*endp) { /* FAILURE, no number on the end */ }
auto i = strtol(endp, &endp, 10);
if (*endp) {/* weird FAILURE, maybe the number was really HUGE and couldn't convert */}
另一种可能性是将字符串放入 stringstream
,然后从流中读取数字(在为流注入一个将除数字以外的所有内容都分类为白色 space 的语言环境之后)。
// First the desired facet:
struct digits_only: std::ctype<char> {
digits_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table() {
// everything is white-space:
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
// except digits, which are digits
std::fill(&rc['0'], &rc['9'], std::ctype_base::digit);
// and '.', which we'll call punctuation:
rc['.'] = std::ctype_base::punct;
return &rc[0];
}
};
然后是读取数据的代码:
std::istringstream virtuallive("Images1234");
virtuallive.imbue(locale(locale(), new digits_only);
int number;
// Since we classify the letters as white space, the stream will ignore them.
// We can just read the number as if nothing else were there:
virtuallive >> number;
当流包含大量数据并且您希望所有流中的数据以相同方式解释时(例如,仅阅读数字,不管它可能包含什么)。
关于从 C++ 中的字符串中提取有符号整数,我遇到了问题。
假设我有一个字符串 images1234
,我如何在不知道 C++ 中最后一个非数字字符的位置的情况下从字符串中提取 1234
。
仅供参考,我已经尝试过 stringstream 以及其他人通过 post 建议的 lexical_cast 但是 stringstream returns 0 而 lexical_cast 停止工作。
int main()
{
string virtuallive("Images1234");
//stringstream output(virtuallive.c_str());
//int i = stoi(virtuallive);
//stringstream output(virtuallive);
int i;
i = boost::lexical_cast<int>(virtuallive.c_str());
//output >> i;
cout << i << endl;
return 0;
}
How can i extract the 1234 from the string without knowing the position of the last non numeric character in C++?
你不能。不过位置不难找:
auto last_non_numeric = input.find_last_not_of("1234567890");
char* endp = &input[0];
if (last_non_numeric != std::string::npos)
endp += last_non_numeric + 1;
if (*endp) { /* FAILURE, no number on the end */ }
auto i = strtol(endp, &endp, 10);
if (*endp) {/* weird FAILURE, maybe the number was really HUGE and couldn't convert */}
另一种可能性是将字符串放入 stringstream
,然后从流中读取数字(在为流注入一个将除数字以外的所有内容都分类为白色 space 的语言环境之后)。
// First the desired facet:
struct digits_only: std::ctype<char> {
digits_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table() {
// everything is white-space:
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
// except digits, which are digits
std::fill(&rc['0'], &rc['9'], std::ctype_base::digit);
// and '.', which we'll call punctuation:
rc['.'] = std::ctype_base::punct;
return &rc[0];
}
};
然后是读取数据的代码:
std::istringstream virtuallive("Images1234");
virtuallive.imbue(locale(locale(), new digits_only);
int number;
// Since we classify the letters as white space, the stream will ignore them.
// We can just read the number as if nothing else were there:
virtuallive >> number;
当流包含大量数据并且您希望所有流中的数据以相同方式解释时(例如,仅阅读数字,不管它可能包含什么)。