首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

迭代器这么用为什么报错呢

2013-08-24 
迭代器这样用为什么报错呢?vectorstring vecstrvecstr.push_back(string(hello))vectorstring::it

迭代器这样用为什么报错呢?



vector<string> vecstr;
vecstr.push_back(string("hello"));
vector<string>::iterator iter = vecstr.begin();
if ( iter )
{
cout << iter->c_str() << endl;
}


报错:
error C2451: conditional expression of type 'std::_Vector_iterator<_Ty,_Alloc>' is illegal

不解啊!
[解决办法]
iterator不能隐式转为bool吧?
是不是想用 if(iter != vecstr.end()) ?
[解决办法]

//最好用这个
 if(iter != vecstr.end()) 

[解决办法]
vector<string>::iterator不是算数类型,不能转换为bool
[解决办法]
引用:


vector<string> vecstr;
vecstr.push_back(string("hello"));
vector<string>::iterator iter = vecstr.begin();
if ( iter != vecstr.end() )
{
cout << iter->c_str() << endl;
}


报错:
error C2451: conditional expression of type 'std::_Vector_iterator<_Ty,_Alloc>' is illegal

不解啊!

这个 iter不能转换为bool类型。

热点排行