在C++中,可以考虑用已经写好的模板来解题:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string line,word;
vector<string> s1;
getline(cin,line);
istringstream stream(line);
while(stream >> word)
{
s1.push_back(word);
}
for(auto it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " " << flush;
}
cout << endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
重要的是:
vector<string> str;
string line, word;
getline(cin, line);
istringstream stream(line);
while(stream >> word)
{
str.push_back(word);
}