第一种方法使用for进行遍历
for(auto &str : input)
{
....
}第二种方法使用transform函数和字符串迭代器进行遍历
transform(input.begin(), input.end(), input.begin(), ::toupper);使用vs2017(15.7)测试通过的完整代码
#include "stdafx.h"
#include "InstallDriver.h"
#include <algorithm>
using namespace std;
int main()
{
string input;
cout << "Input Y(Yes),Install Driver!" << endl;
cin >> input;
for(auto &str : input)
{
str = ::tolower(str);
}
cout << input << endl;
transform(input.begin(), input.end(), input.begin(), ::toupper);
cout << input << endl;
return 0;
}

本文介绍了一种使用C++处理字符串的方法,包括如何利用for循环和transform函数来改变字符串中字符的大小写。通过示例代码展示了两种遍历字符串的方式,并演示了如何将所有字符转换为小写再转换为大写的操作。
3254

被折叠的 条评论
为什么被折叠?



