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;
}
for (i=0; i<length; i++) {
string[i] = toupper(string[i]);
}
我的C++比较旧不支持C++ 11/14
toupper只能每次转一个,我写了一个支持转string的。