一种方法:范围for语句:
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
string word;
vector<string> text;
while (cin >> word)
{
text.push_back(word);
}
for (auto& c : text)
{
for (auto& d : c)
{
d = toupper(d);
}
cout << c << endl;
}
return 0;
}
另一种写法,用下标来写:
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
string word;
vector<string> text;
while (cin >> word)
{
text.push_back(word);
}
for (vector<string>::size_type i = 0; i < text.size(); i++)
{
for (string::size_type j = 0; j < text[i].size(); j++)
{
text[i][j] = toupper(text[i][j]);
}
cout << text[i] << endl;
}
return 0;
}
再一种写法,迭代器来写:
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
string word;
vector<string> text;
while (cin >> word)
{
text.push_back(word);
}
for (vector<string>::iterator iti = text.begin(); iti != text.end(); ++iti)
{
for (string::iterator itj = (*iti).begin(); itj != (*iti).end(); ++itj)
{
*itj = toupper(*itj);
}
cout << *iti << endl;
}
return 0;
}