【发布时间】:2017-06-02 23:32:18
【问题描述】:
这是一个向量的基本二分搜索函数。我想访问一个对象的 get 函数,但我得到了错误。
bool binFindInVec(vector<Client> *vec,string sur){
int from,to,pos;
from = 0;
to = vec->size()-1;
while(from<=to){
pos = (from+to)/2;
if(vec[pos]->getSurname() == sur){
return true;
}
else if(vec[pos]->getSurname() > sur){
to = pos-1;
}
else{
from = pos + 1;
}
}
return NULL;
}
错误:
在函数'bool binFindInVec(std::vector*, std::string)'中:
176 14 [错误] '->' 的基本操作数具有非指针类型 'std::vector'
179 19 [错误] '->' 的基本操作数具有非指针类型'std::vector'
【问题讨论】:
-
你可能想要
(*vec)[pos].getSurname()。 -
另外,您可以通过传递引用来简化事情:
bool binFindInVec(vector<Client>& vec, string sur){。然后你可以使用:vec[pos].getSurname(). -
您可能还想考虑一下为什么需要将指针传递给向量而不是引用。
-
由于
vec是vector<Client> *,vec[x](又名*(vec + x))是vector<Client>。
标签: c++ pointers vector operator-keyword