程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> vector string-迭代器和string的相關問題

vector string-迭代器和string的相關問題

編輯:編程綜合問答
迭代器和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不是標准庫類型嗎??

最佳回答:


 // 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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved