C++将字符串内的数字分离出来,字符串内包含double和int
时间: 2023-07-15 14:10:48 浏览: 47
可以使用字符串流 stringstream 来实现。
具体步骤如下:
1. 包含头文件 #include <sstream>
2. 定义一个 stringstream 对象
3. 将字符串输入到 stringstream 对象中
4. 使用 >> 操作符从 stringstream 对象中提取数字,直到无法继续提取
5. 将提取出的数字存储到一个容器中(如 vector)
6. 处理存储在容器中的数字
代码示例:
```c++
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::string s = "123.45 678 90.1";
std::stringstream ss(s);
std::vector<double> nums;
double num;
while (ss >> num) {
nums.push_back(num);
}
for (double n : nums) {
std::cout << n << std::endl;
}
return 0;
}
```
输出结果:
```
123.45
678
90.1
```
相关问题
C++将字符串内的数字分离出来,字符串内包含double和int和string
在C++中,可以使用stringstream和isdigit函数将字符串内的数字分离出来。具体步骤如下:
1.引入头文件#include <sstream>
2.将字符串放入stringstream对象中
3.使用循环逐个字符读取stringstream对象中的字符,并使用isdigit函数判断是否为数字
4.将连续的数字字符组成一个字符串,转换成数字类型并存储起来
下面是一个示例代码,可以将字符串中的数字分离出来并存储在vector<double>和vector<int>中:
```
#include <iostream>
#include <vector>
#include <sstream>
#include <ctype.h>
using namespace std;
int main()
{
string str = "12.3 abc 45 6.7 def 89 xyz";
stringstream ss(str);
vector<double> doubles;
vector<int> ints;
string tmp;
while (ss >> tmp)
{
double d;
int i;
stringstream ss_tmp(tmp);
if (ss_tmp >> d)
{
doubles.push_back(d);
}
else if (ss_tmp >> i)
{
ints.push_back(i);
}
for (int j = 0; j < tmp.size(); j++)
{
if (isdigit(tmp[j]))
{
string num_str;
num_str += tmp[j];
while (j < tmp.size() - 1 && isdigit(tmp[j + 1]))
{
j++;
num_str += tmp[j];
}
if (sscanf(num_str.c_str(), "%d", &i) == 1)
{
ints.push_back(i);
}
else if (sscanf(num_str.c_str(), "%lf", &d) == 1)
{
doubles.push_back(d);
}
}
}
}
for (int i = 0; i < doubles.size(); i++)
{
cout << "Double: " << doubles[i] << endl;
}
for (int i = 0; i < ints.size(); i++)
{
cout << "Int: " << ints[i] << endl;
}
return 0;
}
```
在上面的示例代码中,先将字符串放入stringstream对象中,然后使用while循环逐个读取stringstream对象中的字符串,使用stringstream对象分别尝试将其转换成double和int类型。如果可以转换成功,则直接存储到vector<double>或vector<int>中。如果不能转换成功,则使用isdigit函数判断是否为数字字符,如果是数字字符,则将连续的数字字符组成一个字符串,然后使用sscanf函数尝试将其转换成double或int类型,如果转换成功,则存储到vector<double>或vector<int>中。最后遍历vector<double>和vector<int>输出结果。
c++如何将字符串转成数字
回答: 在C++中,有几种方法可以将字符串转换为数字。一种方法是使用to_string函数,它可以将整数、浮点数和双精度数转换为字符串。例如,可以使用to_string函数将整数i转换为字符串,如下所示:cout<<to_string(i)<<endl; \[1\]另一种方法是使用stringstream类,它可以将数字转换为字符串。可以使用stringstream类的对象将数字插入到流中,然后使用str()函数将流转换为字符串。例如,可以使用stringstream类将双精度数d转换为字符串,如下所示:stringstream ss; ss<<d; return ss.str(); \[2\]在C语言的风格中,可以使用atoi函数将字符串转换为整数,使用atof函数将字符串转换为双精度数。例如,可以使用atoi函数将字符串s1转换为整数i,如下所示:int i = atoi(s1.c_str()); \[3\]同样,可以使用atof函数将字符串s2转换为双精度数d,如下所示:double d = atof(s2.c_str()); \[3\]
#### 引用[.reference_title]
- *1* *3* [c++ 字符串与数字相互转换](https://blog.csdn.net/bitcarmanlee/article/details/125129195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [c/c++ 数字转成字符串, 字符串转成数字](https://blog.csdn.net/touzani/article/details/1623850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]