std::vectorcard;
class Information
{ ...... };
Information *Search()
{
int i=1;
string tempname;
cout<<"请输入姓名:";
cin>>tempname;
Information * temp;
temp=card.begin();
//无法从“std::_Vector_iterator<_Myvec>”转换///为“Information *”
while(i<=card.size())
{
if((temp->Get_Name())==tempname)
{ return temp; }
temp++;
i++;
}
return NULL;
}
temp=card.begin();
//无法从“std::_Vector_iterator<_Myvec>”转换///为“Information *”
请问如何很正

请教一个关于vector函数的问题?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- charlie_cao 2016-02-22 06:33关注
begin返回的是迭代器,不能直接赋值给Information指针。
下面的循环查找逻辑也错了,可以用下面一种写法Information *temp = NULL; std::vector<Information*>::iterator it = card.begin(); while(it < card.end()) { temp = *it; if((temp->Get_Name()) == tempname) { return temp; } ++it; } return NULL
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报