C++ 从字符串中提取整数
从字符串中提取整数并将其保存到整数数组的最佳和最短方法是什么 示例字符串“6586513565234 6532$!@” 我试着看了一些其他的帖子,但找不到关于这个具体问题的帖子。。。C++ 从字符串中提取整数,c++,string,int,C++,String,Int,从字符串中提取整数并将其保存到整数数组的最佳和最短方法是什么 示例字符串“6586513565234 6532$!@” 我试着看了一些其他的帖子,但找不到关于这个具体问题的帖子。。。 一些帮助和解释将非常有用。由于此处分隔符的复杂性(您似乎有空格和非数字字符),我将使用boost库中提供的字符串拆分: 这允许您使用正则表达式作为分隔符进行拆分 首先,选择定界符,它是一个正则表达式: boost::regex delim(" "); // I have just a space here, bu
一些帮助和解释将非常有用。由于此处分隔符的复杂性(您似乎有空格和非数字字符),我将使用boost库中提供的字符串拆分: 这允许您使用正则表达式作为分隔符进行拆分 首先,选择定界符,它是一个正则表达式:
boost::regex delim(" "); // I have just a space here, but you could include other things as delimiters.
然后摘录如下:
std::string in(" 65 865 1 3 5 65 234 65 32 ");
std::list<std::string> out;
boost::sregex_token_iterator it(in.begin(), in.end(), delim, -1);
while (it != end){
out.push_back(*it++);
}
std::字符串输入(“65865135652346532”);
列出来;
sregex_令牌_迭代器(in.begin()、in.end()、delim,-1);
while(it!=结束){
向外推。向后推(*it++);
}
你可以看到我把它简化成了一个字符串列表。如果需要对整数数组执行整个步骤,请告诉我(不确定需要什么数组类型);如果您想使用boost方法,也很乐意将其包括在内。您可以使用一个工具来保存字符串数据并将其读出
使用典型的C++ IOSWATH机制为整数:
#include <iostream>
#include <sstream>
int main(int argc, char** argv) {
std::stringstream nums;
nums << " 65 865 1 3 5 65 234 65 32 #$!@#";
int x;
nums >> x;
std::cout <<" X is " << x << std::endl;
} // => X is 65
查看对int的读取是否成功 我喜欢为此使用
istringstream
istringstream iss(line);
iss >> id;
由于它是一个流,您可以像cin
一样使用它。默认情况下,它使用空格作为分隔符。您只需将其包装在一个循环中,然后将生成的字符串
转换为int
似乎这一切都可以通过
std::stringstream来完成:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
std::string str(" 65 865 1 3 5 65 234 65 32 #$!@#");
std::stringstream ss(str);
std::vector<int> numbers;
for(int i = 0; ss >> i; ) {
numbers.push_back(i);
std::cout << i << " ";
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
标准:字符串str(“6586513565234 6532$!”;
std::stringstream ss(str);
std::向量数;
对于(int i=0;ss>>i;){
数字。推回(i);
std::cout i;){
数字。推回(i);
std::您的字符串是否总是以#
开头并放在字符串的末尾?我的字符串实际上都是整数,但以一个非数字字符结尾,例如:“1 4 5 2 54 65 3246 53490 80 9”试着看看stringstream?或者看看topcoder的教程?我想这样你可以学到更多:这篇文章可能很有用:在这种情况下,单独使用stringstream
似乎是行不通的“65 865 1 3 5 65 234 65 32#$!@”
啊,现在我明白了。我会将此解决方案添加到任何需要它的人的底部外壳中。
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
std::string str(" 65 865 1 3 5 65 234 65 32 #$!@#");
std::stringstream ss(str);
std::vector<int> numbers;
for(int i = 0; ss >> i; ) {
numbers.push_back(i);
std::cout << i << " ";
}
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct not_digit {
bool operator()(const char c) {
return c != ' ' && !std::isdigit(c);
}
};
int main() {
std::string str(" 65 865 1 3 5 65 234 65 32 #$!@# 123");
not_digit not_a_digit;
std::string::iterator end = std::remove_if(str.begin(), str.end(), not_a_digit);
std::string all_numbers(str.begin(), end);
std::stringstream ss(all_numbers);
std::vector<int> numbers;
for(int i = 0; ss >> i; ) {
numbers.push_back(i);
std::cout << i << " ";
}
return 0;
}