读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象 中每个单词转化为大写字母。输出 vector 对象中转化后的元素,每八个单词为一行输出
时间: 2024-05-15 14:17:54 浏览: 5
好的,根据您的要求,您可以使用以下C++代码来实现所述功能:
```
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void toUpperCase(vector<string>& words) {
for (int i = 0; i < words.size(); i++) {
for (int j = 0; j < words[i].length(); j++) {
words[i][j] = toupper(words[i][j]);
}
}
}
void printVector(vector<string>& words) {
for (int i = 0; i < words.size(); i++) {
cout << words[i] << " ";
if ((i + 1) % 8 == 0) {
cout << endl;
}
}
}
int main()
{
vector<string> words;
string input;
getline(cin, input);
string separator = " ";
size_t pos = 0;
while ((pos = input.find(separator)) != string::npos) {
string token = input.substr(0, pos);
words.push_back(token);
input.erase(0, pos + separator.length());
}
words.push_back(input);
toUpperCase(words);
printVector(words);
return 0;
}
```
请问您有什么其他问题需要我回答吗?
相关推荐
![](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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)