【发布时间】:2012-05-11 10:55:02
【问题描述】:
我想从向量中返回一个对象的引用,并且该对象在一个迭代器对象中。我该怎么做?
我尝试了以下方法:
Customer& CustomerDB::getCustomerById (const string& id) {
vector<Customer>::iterator i;
for (i = customerList.begin(); i != customerList.end() && !(i->getId() == id); ++i);
if (i != customerList.end())
return *i; // is this correct?
else
return 0; // getting error here, cant return 0 as reference they say
}
代码中customerList是一个客户向量,getId函数返回客户的id。
*i 正确吗?以及如何返回 0 或 null 作为参考?
【问题讨论】:
标签: c++ reference vector null iterator