C++ Primer(第五版)练习3.22

来源:互联网 发布:eureka服务注册源码 编辑:程序博客网 时间:2024/06/05 18:45

这题我遇到的一些不懂的地方:

先贴上能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' 这是错误原因 不太懂
0 0
原创粉丝点击