I want to return a reference of an object from a vector, and the object is in an iterator object. How can I do that?
I tried the following:
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
}
In the code, customerList is a vector of customers, and the function getId returns the id of the customer.
は*i
正しいですか?また、参照として 0 または null を返すにはどうすればよいですか?