c++ - 对向量字符串中的第一个字符使用 toupper
问题描述
如何使用toupper()
将字符串向量中每个元素的第一个字母转换为大写?
这是我尝试过的...
string word;
vector<string> text;
while(cin >> word){
text.push_back(word);
}
for(decltype(text.size()) i = 0; i != text.size()) {
text[0] = toupper(text);
}
解决方案
for(auto& s : text)
if (!s.empty()) s[0] = std::toupper(s[0]);
或者更花哨:
std::transform(std::istream_iterator<std::string>{std::cin}, {},
std::back_inserter(text),
[](auto s) {
if (!s.empty()) s[0] = std::toupper(s[0]);
return s;
});
推荐阅读
- android - Firebase 数据库函数规则
- powershell - 手动 zip 和 powershell zip 的区别
- angular - ngx-bootstrap BsModalService 上的 Jest 测试中断
- angular - 仅存储 Firebase 存储下载 URL 令牌(不是完整路径)是否安全?
- javascript - 将 Electron (NodeJS) 包导入 HTML 文件
- c++ - unordered_map priority_queue 比较器
- php - 致命错误:允许的内存大小...字节已用尽
- vue.js - 与 Index 进行数据绑定的语法
- php - PHP MySQL 准备好的语句不从视图中获取
- python - 将python-trio中的信号量和时间限制与asks http请求相结合