Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;std::list::front()返回空列表吗?_C++_Null_Stdlist - Fatal编程技术网

C++ c++;std::list::front()返回空列表吗?

C++ c++;std::list::front()返回空列表吗?,c++,null,stdlist,C++,Null,Stdlist,如果我有密码: struct Test { int x = 10; }; int main() { std::list<Test> linkedList; std::cout << linkedList.front().x << std::endl; } --- out -> 0 struct测试 { int x=10; }; int main() { std::列表链接列表; std::coutlinkedList是一个空的列表。调用

如果我有密码:

struct Test
{
  int x = 10;
};

int main()
{
  std::list<Test> linkedList;
  std::cout << linkedList.front().x << std::endl;
}
---

out -> 0
struct测试
{
int x=10;
};
int main()
{
std::列表链接列表;

std::cout
linkedList
是一个空的
列表
。调用它会导致,意味着任何事情都是可能的

返回对容器中第一个元素的引用

在空容器上调用front未定义


注意:强制检查empty是所有呼叫方必须支付的费用,这些呼叫方知道列表不是空的,不应该进行此测试。C++的基本原则之一是你不需要为不需要的东西付费。你可以获得很快的速度,但你也必须小心。不要指望得到一个如果你现在得到零,那可能只是一个愚蠢的运气。明天你可以得到42。或者程序可能崩溃。或者命令津巴布韦上的核打击。“C++不是一个安全的语言;如果你不遵循语言的许多规则,那么坏事就会发生。”亚当约翰斯顿“你怎么回来?”undefined“”-您没有。引用的文本并不是说对空列表调用
front()
返回undefined。而是说调用
front()的行为在空列表中,“/CODE>”是未定义的,因此它可以随便做任何它想要的。@ AdamJohnston在您自己的代码中,您可以做任何您想要的。如果您选择支持安全性超过速度并且要检查,抛出异常(C++标准库使用的是它进行边界检查的情况)。更通用。列表的用户可以捕获异常、清理并继续,或者让异常终止程序。
exit
只是终止程序。