当前位置:网站首页>【无标题】
【无标题】
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();
边栏推荐
- [Hardware Engineer] about signal level driving capability
- 【PHP伪协议】源码读取、文件读写、任意php命令执行
- 04.寻找两个正序数组的中位数
- Page table cache of Linux kernel source code analysis
- 基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
- 电子产品EMC不合格,如何整改?
- 咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
- 网上开期货账户安全吗?手续费怎么申请才低?
- Chapter VI succession
- "Digital security" alert NFT's seven Scams
猜你喜欢

Outlook tutorial, how to search for calendar items in outlook?

Multi tenant software development architecture

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...

2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析

如何看一本书

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

How to fix the first row title when scrolling down in Excel table / WPS table?

ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性

EasyUI drop-down box, add and put on and off shelves of products
随机推荐
02.两数相加
An article about ultrasonic humidifier
Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
Lvgl 7.11 tileview interface cycle switching
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Don't believe these "rumors" in the process of preparing for the exam!
Outlook 教程,如何在 Outlook 中搜索日历项?
stm32F407------SPI
Add batch delete
[Hardware Engineer] about signal level driving capability
How to prevent the unburned gas when the city gas safety is alarmed again?
【硬件工程师】元器件选型都不会?
【硬件工程师】DC-DC隔离式开关电源模块为什么会用到变压器?
Frustrated Internet people desperately knock on the door of Web3
电子产品“使用”和“放置”哪个寿命更长??
【Cadence Allegro PCB设计】永久修改快捷键(自定义)~亲测有效~
2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
04. Find the median of two positive arrays
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part5 end): information retrieval and result assembly