c++ 怎么用空格分隔字符串?

比如是"aa 12 2.3"怎么让string first="aa", int second=12, double third=2.3?
2025-04-10 07:15:59
推荐回答(1个)
回答1:

#include 
#include 
#include 

int main()
{
using namespace std;

istringstream iss("aa 12 2.3");
string first;
int second;
float third;

// istringstream 用法和 cin 相同
iss >> first >> second >> third ;

cout << third << "-" << first << "-" << second << endl;

return 0;
}