我有一个列表
或者我也应该检查是否为空?
std::list<T *> *l;
,这个列表不为空并且有一些值。我的问题是如何正确访问列表项?我不需要迭代整个列表,只想获取第一个项目。std::list<T*>::iterator it = l->begin();
if (it != l->end())
{
// accessing T
int value = (*it)->value(); // Is this safe?
}
或者我也应该检查是否为空?
if (it != l->end() && (*it))
{
// accessing T
int value = (*it)->value();
}
std::list<T *>
而不是std::list<T>
? - LihO