G____yiyi 2015-05-29 04:50 采纳率: 62.5%
浏览 1737
已采纳

迭代器和string的相关问题

 vector<string> text{"hello world"};
for(auto it = text.begin(); it != text.end() && ! it->empty(); ++it)
            {
                        *it = toupper(*it);
                        cout<< *it << endl;
            }

这里有个问题,toupper()只接受int的参数,而vector的基础元素是string所以无法使用,有没有办法在循环中使用toupper()

第二:当我将text的类型改为string时,循环的判断条件 ! it->empty() 编译器显示表达式应该包含指向类型的指针,why??难道string不是一个类型吗,string不是标准库类型吗??

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-05-29 05:08
    关注
     // app1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <vector>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string toupper(string s)
    {
        string r = "";
        for (int i = 0; i < s.length(); i++)
            r += (char)toupper(s.c_str()[i]);
        return r;
    }
    
    int main(int argc, char* argv[])
    {
        vector<string> text;
        text.push_back("hello world");
        for (vector<string>::iterator it = text.begin(); it != text.end() && ! it->empty(); ++it)
        {
            string s = *it;
            cout<< toupper(s) << endl;
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?