代码目的是把迭代器第一个 string 类型元素的第一段不包括空格的字符全改为大写。
期望输出:SOME string oh
实际输出:SOME string oh
代码输出确实是正确的,但是总觉得代码里太多解引用(*it)写起来很繁琐,不知道大佬们怎么解决。如题。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> s{ "some string"," oh" };
for (auto it = s.begin(); it != s.end() && !it->empty(); ++it) {
if (it == s.begin()) { //想把(*it)改成字符串直接使用;
for (decltype((*it).size()) index = 0; index != (*it).size() && !isspace((*it)[index]); index++) {
(*it)[index] = toupper((*it)[index]);
}
}
cout << (*it) << " ";
}
return 0;
}