C++ Primer(第五版)練習3.22

這題我遇到的一些不懂的地方:

先貼上能Debug的代碼如下,這是參考網絡上的寫的

————————————————————————————————————————

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	string text = ("Hello,my name is YJ1an.Nice to meet you!");
	for (auto it = text.begin(); it != text.end(); ++it)
		*it = toupper(*it);
	cout << text << endl;
	system("pause");
	return 0;
}


——————————————————————————————————————————


但我如果想用vector<string>定義 text,就會出現很多問題:(如下代碼是有錯誤的)

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<string> text;
	string word;
	while (cin >> word)
		text.push_back(word);
	for (auto it = text.begin(); it != text.end() && !it->empty(); ++it)
		*it = toupper(*it);
	for (auto i : text)
		cout << i << " ";
	system("pause");
	return 0;
}


error C2664: 'int toupper(int)': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'int' 這是錯誤原因 不太懂
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章