读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象中每个单词转化为大写字母。输出 vector 对象中转化后的元素,每八个单词为一行输出。
时间: 2024-04-29 08:26:27 浏览: 140
好的,您的问题已经记录下来了。以下是您需要的代码实现:
```C++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
std::vector<std::string> words;
std::string input;
std::cout << "请输入一段文本:" << std::endl;
// 读取文本到 vector 对象
while (std::cin >> input) {
words.push_back(input);
}
// 把 vector 对象中每个单词转化为大写字母
std::transform(words.begin(), words.end(), words.begin(),
[](std::string word) {
std::transform(word.begin(), word.end(), word.begin(),
[](unsigned char c){ return std::toupper(c); });
return word;
});
// 每八个单词为一行输出
int count = 0;
for (const auto& word : words) {
std::cout << word << " ";
if (++count % 8 == 0) {
std::cout << std::endl;
}
}
return 0;
}
```
使用方法很简单,只需要复制上述代码并粘贴到您的 IDE 编辑器中即可。运行程序时,会提示您输入一段文本,输入完后程序就会实现您的要求,并输出结果。希望能够帮助到您!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)