一段英文单词由若干单词组成,编写程序提取其中的所有单词。请实现由用 户输入英文,利用字符串存储英文,提取后的单词用向量容器存储。
时间: 2024-05-10 09:17:44 浏览: 16
以下是一个C++的示例代码,可以实现输入英文句子,提取其中的所有单词,并将单词存储在向量容器中:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string sentence;
vector<string> words;
cout << "请输入英文句子:";
getline(cin, sentence);
// 使用字符串流将句子分解成单词
stringstream ss(sentence);
string word;
while (ss >> word) {
words.push_back(word);
}
// 输出提取出的单词
cout << "提取出的单词有:" << endl;
for (auto w : words) {
cout << w << endl;
}
return 0;
}
```
运行程序后,可以输入英文句子,程序将会输出提取出的所有单词。例如,输入句子 "This is a test sentence.",程序输出的结果如下:
```
提取出的单词有:
This
is
a
test
sentence.
```