以下代码是从c ++入门书中复制的。当我把它放在我的编译器(MS studio express)时,我得到一个转换错误,说它不能从类型字符串转到int类型。在for语句中我取消引用迭代器,所以我不明白为什么我得到错误。任何帮助?
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> svec, svec1;
string s1;
while (cin >> s1){ svec.push_back(s1); }
for (auto a1 = svec.begin(); a1 != svec.end() && !isspace(*a1); ++a1){
*a1 = toupper(*a1);
}
system("pause");
return 0;
}
答案 0 :(得分:1)
*a1
是string
。您无法执行isspace(*a1)
因为isspace
需要字符,而不是字符串。与toupper
相同。
你的循环看起来像一个循环的弗兰肯斯坦缝合来迭代向量并用循环遍历字符串的每个字符。
我猜你要转换所有字符串,在这种情况下代码将是:
for ( auto str = svec.begin(); str != svec.end(); ++str )
for (auto a1 = str->begin(); a1 != str->end(); ++a1 )
*a1 = toupper(*a1);
NB。如果您使用的是auto
,则表示您的编译器支持C ++ 11,因此使用基于范围的for
循环会更简单。