【发布时间】:2015-06-17 22:54:29
【问题描述】:
我正在尝试使用std::stringstream 从myString1 中提取值,如下所示:
// Example program
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string myString1 = "+50years";
string myString2 = "+50years-4months+3weeks+5minutes";
stringstream ss (myString1);
char mathOperator;
int value;
string timeUnit;
ss >> mathOperator >> value >> timeUnit;
cout << "mathOperator: " << mathOperator << endl;
cout << "value: " << value << endl;
cout << "timeUnit: " << timeUnit << endl;
}
输出:
mathOperator: +
value: 50
timeUnit: years
在输出中你可以看到我成功提取了我需要的值、数学运算符、值和时间单位。
有没有办法对myString2 做同样的事情?也许在一个循环中?我可以提取数学运算符,值,但时间单位只是提取其他所有内容,我想不出办法解决这个问题。非常感谢。
【问题讨论】:
标签: c++ stream formatted-input