>s1){svec.push_back(s1)} 对于(auto a1=svec.begin();a1!=svec.end()&&&!isspace(*a1);++a1){ *a1=toupper(*a1); } 系统(“暂停”); 返回0; },c++,iterator,C++,Iterator" /> >s1){svec.push_back(s1)} 对于(auto a1=svec.begin();a1!=svec.end()&&&!isspace(*a1);++a1){ *a1=toupper(*a1); } 系统(“暂停”); 返回0; },c++,iterator,C++,Iterator" />

c++;初级读本第3章迭代器转换错误 下面的代码是从C++入门书中复制的。当我把它放在我的编译器(MS Studio express)中时,我得到一个转换错误,表示它不能从string类型转换为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; } #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; int main(){ 向量svec,svec1; 字符串s1; while(cin>>s1){svec.push_back(s1)} 对于(auto a1=svec.begin();a1!=svec.end()&&&!isspace(*a1);++a1){ *a1=toupper(*a1); } 系统(“暂停”); 返回0; }

c++;初级读本第3章迭代器转换错误 下面的代码是从C++入门书中复制的。当我把它放在我的编译器(MS Studio express)中时,我得到一个转换错误,表示它不能从string类型转换为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; } #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; int main(){ 向量svec,svec1; 字符串s1; while(cin>>s1){svec.push_back(s1)} 对于(auto a1=svec.begin();a1!=svec.end()&&&!isspace(*a1);++a1){ *a1=toupper(*a1); } 系统(“暂停”); 返回0; },c++,iterator,C++,Iterator,*a1是一个字符串。您不能执行isspace(*a1),因为isspace需要的是字符,而不是字符串。同样适用于toupper 你的循环看起来像是一个循环的弗兰肯斯坦式缝合,循环遍历向量,循环遍历字符串的每个字符 我猜您希望转换所有字符串,在这种情况下,代码为: for ( auto str = svec.begin(); str != svec.end(); ++str ) for (auto a1 = str->begin(); a1 != str->end(); ++a

*a1
是一个
字符串
。您不能执行
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);

注意。如果您使用的是
auto
,这意味着您的编译器具有C++11支持,因此对于
循环使用基于范围的
会更简单。

a1!=svec.end
应该是
a1!=svec.end()
。missing
()
我添加了missing(),但仍然有相同的错误。@user3089453到底是哪个错误?准确地说,在你的问题中逐字逐句地张贴错误信息。“表示无法从字符串类型转换为int类型时出错。”wishy washy重新措辞不算数。为什么所有的
*a1
解引用尝试?!?这行不通。您正在取消对
a1
字符串向量迭代器的引用,并将其传递给
isspace
,该迭代器需要一个int。您确定不需要在该迭代器中进行另一次迭代来迭代实际字符串而不是向量吗?重点是将字符串转换为大写?