我试图理解 const_iterator 的含义。我有以下示例代码:
void CustomerService::RefreshCustomers()
{
for(std::vector<Customer*>::const_iterator it = customers_.begin();
it != customers_.end() ; it ++)
{
(*it)->Refresh();
}
}
Refresh()
是Customer
类中的一个方法,它没有被定义为 const。起初我以为 const_iterator 应该禁止修改容器的元素。但是,此代码可以毫无怨言地编译。这是因为发生了额外的间接级别吗?const_iterator 究竟是做什么/意味着什么?
更新
在这种情况下,使用 const_iterator 是最佳做法吗?