C++中string与vector

先看几段代码:

 1     string str1 ("aaaaaaaaaa");
 2     for(auto it1 = str1.begin(); it1 != str1.end() && !str1.empty(); ++it1 ) {
 3         *it1 = toupper(*it1);
 4     }
 5 
 6     string str1 ("aaaaaaaaaa");
 7     for(auto it = str1.begin(); it != str1.end() && !it->empty(); ++it ) {   //error1
 8         *it1 = toupper(*it1);
 9     }
10 
11     vector<string> vstr{"aaa","bbb","ccc"};
13     for (auto it = vstr.begin(); it != vstr.end() && !it->empty(); ++it) {
14         *it = toupper(*it);       //error2
15     }

Error1:第7行:expression must have pointer-to-class type

意思是:it->empty()这个表达式的i必须是要指向一个类的指针。it解引用后为char,是一个基本数据类型,不是类,也不存在有empty方法。

              string在c++中可以是一个封装号的字符串类。

Error2:第14行:no instance of overloaded function "toupper" matches the argument list

            toupper里面处理的是字符,而it = vstr.begin(), it指向的容器Vstr里面的第一个元素即字符串。

posted @ 2020-10-13 11:07  朱果果  阅读(946)  评论(0编辑  收藏  举报