terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size() 报错

记录下今天的leetcode报错信息

题目:654.最大二叉树

给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:

创建一个根节点,其值为 nums 中的最大值。
递归地在最大值 左边 的 子数组前缀上 构建左子树。
递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums 构建的 最大二叉树 。

image-20220820151559802
链接:https://leetcode.cn/problems/maximum-binary-tree
一开始的解:

报错是因为这里构造函数写错了

改成 image-20220820151840364

即可
是因为不使用new创建对象时,对象的内存空间是在栈中的,其作用范围只是在函数内部,函数执行完成后就会调用析构函数,删除该对象,而new创建对象是在堆中的
报错超出max_size可能是超出栈容量了吧。
附链接:C++两种创建对象方法的区别https://blog.csdn.net/linZinan_/article/details/117377310

posted @ 2022-08-20 15:19  专心写Bug  阅读(1967)  评论(0)    收藏  举报