当前位置:网站首页>【无标题】
【无标题】
2022-07-25 17:29:00 【Wildcraner】
总结vector的几个常见错误
1、vector<vector>& matrix = []时
vector<int> abc(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {
};
}
以上情况当martix是空时,会报错:
Line 1033: Char 9: runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator>’ (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/stl_vector.h:1043:9
原因是int m = matrix[0].size();这句话中的matrix[0]是不存在的所以不能通过
修改方式有两个:
1、使用empty()
vector<int> abc(vector<vector<int>>& matrix) {
if (matrix.empty()) {
return {
};
}
int n = matrix.size();
int m = matrix[0].size();
2、先判断
vector<int> abc(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {
};
}
int n = matrix.size();
int m = matrix[0].size();
边栏推荐
- 栈的顺序存储结构,链式存储结构及实现
- I'm also drunk. Eureka delayed registration and this pit!
- 第四章:操作符
- Enumeration classes and magic values
- 01.两数之和
- Chapter III data types and variables
- 理财有保本产品吗?
- Page table cache of Linux kernel source code analysis
- How to rectify the unqualified EMC of electronic products?
- [cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
猜你喜欢

Chapter III data types and variables

【南京航空航天大学】考研初试复试资料分享

I'm also drunk. Eureka delayed registration and this pit!

Random talk on generation diffusion model: DDPM = Bayesian + denoising

博云容器云、DevOps平台斩获可信云“技术最佳实践奖”

用秩讨论线性方程组的解/三个平面的位置关系

Rainbow plug-in extension: monitor MySQL based on MySQL exporter

2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
![[Hardware Engineer] can't select components?](/img/bd/fdf62b85c082f7e51bf44737f1f787.png)
[Hardware Engineer] can't select components?

「数字安全」警惕 NFT的七大骗局
随机推荐
Briefly describe the implementation principle of redis cluster
[cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
Is the online account opening of Founder futures reliable and safe?
POWERBOARD coco! Dino: let target detection embrace transformer
第三章、数据类型和变量
吴恩达机器学习编程作业无法暂停pause问题解决
栈的顺序存储结构,链式存储结构及实现
世界各地的标志性建筑物
01.两数之和
[solution] the Microsoft edge browser has the problem of "unable to access this page"
【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题
[cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
新版selenium4.3在egde浏览器的无头模式
动态规划题目记录
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Is there a principal guaranteed product for financial management?
Go language series: where does go come from and where will go?
四六级
Wu Enda logistic regression 2
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart