我可以使用c
的參考變量(例如,Customer &c
)嗎?如果是這樣如何?這是更喜歡使用指針變量?C++參考vs指針for循環內變量?
for(std::vector<Customer*>::const_iterator it = customers_.begin();
it != customers_.end() ; it ++)
{
Customer *c = (*it);
c->PrintName();
...
}
爲什麼不直接使用迭代器? IT-> PrintName(); – Ferruccio
@Ferruccio:因爲迭代器引用了一個指針,而不是一個對象。你必須做'(* it) - >' –
我明白了。我錯過了。 – Ferruccio