1 數組遍歷
#include <iostream>
using namespace std;
int main()
{
string s("Hello World");
int length=s.length();
for(int i=0;i<length;i++){
s[i]=toupper(s[i]);
}
cout<<s<<endl;
return 0;
}
2 使用引用
#include <iostream>
using namespace std;
int main()
{
string s("Hello World");
int length = s.length();
for (char& c : s) {
c = toupper(c);
}
cout << s << endl;
return 0;
}
迭代器
#include <iostream>
using namespace std;
int main()
{
string s("Hello World");
for (auto it=s.begin(); it!=s.end(); it++)
{
*it = toupper(*it);
}
cout << s << endl;
return 0;
}