以下に示すように、myString1
使用して値を抽出しようとしています。std::stringstream
// 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
ますか? もしかしてループ中?数学演算子である値を抽出することはできますが、時間単位は単に他のすべてを抽出するだけであり、それを回避する方法が思いつきません。とても有難い。