網站首頁 編程語言 正文
1.數組
1.1 數組的聲明格式
1.1.1. 格式一
數據類型 數組變量名稱[數組元素個數]{初始化值, 初始化值...}
int a[10]{};
1.1.2. 格式二
變量類型 數組名[]{初始化, 初始化...}
int a []{1, 2, ,3};//編譯器會自動將a聲明為一個有3個元素的數組
1.2.數組的本質
數組的本質是按照所申請的數據類型,向操作系統申請了一段連續的內存空間。兩個特征:
連續的有序的
2. 數組遍歷
2.1. 常規
int studentId[]{101, 102, 103, 104, 105, 106}; //數組遍歷 for (int i = 0; i < sizeof(studentId) / sizeof(int); i++) std::cout << studentId[i] << std::endl;
2.2. C++ 11 遍歷新語法
2.2.1. 格式一
for(變量類型 變量名稱:數組)
循環內容;
for (int i : studentId) std::cout << i << std::endl; //i 不再是索引,而是數組內的元素;
2.2.2. 格式二
for(auto 變量名稱:數組)
循環內容;
for (auto i : studentId) std::cout << i << std::endl; //i 不再是索引,而是數組內的元素;
3. 多維數組
3.1 多維數組的本質
3.2. 聲明格式
類型 變量名稱[元素個數][元素個數]...[元素個數]{};
int studentId[3][5]{};
多維數組的初始化:
int studentId[3][5] { {101, 102, 103, 104, 105, 106}, {201, 202, 203, 204, 205, 206}, {301, 302, 303, 304, 305, 306} };
3.3. 多維數組的遍歷
int studentId[3][5]{ {1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}, {21, 22, 23, 24, 25} }; for (int x = 0; x < 3; x++) { int count{}; for (int y: studentId[x]) { count++; std::cout << x + 1 << "班第" << count << "個同學的學號是" << y << std::endl; } }
4. 數組安全
由于數組的本質是向操作系統申請了一塊內存,因此越界的數組將會訪問到不該訪問的地址,這種越界將會造成程序奔潰,BUG,錯誤,更可怕的是,數組越界漏洞,可能會讓攻擊著拿到操作系統的控制權。
總結
原文鏈接:https://blog.csdn.net/Nine_Yao/article/details/122965021
相關推薦
- 2023-07-22 測試springboot log4j2異步配置是否生效
- 2022-02-10 Kotlin實現Android系統懸浮窗詳解_Android
- 2022-06-08 nacos項目啟動報錯:Connection refused: no further informa
- 2022-06-21 C++簡明分析臨時對象是什么_C 語言
- 2022-11-18 C++簡單實現shared_ptr的代碼_C 語言
- 2022-07-08 圖文詳解Nginx多種匹配方式_nginx
- 2022-04-27 Python中的元組(Tuple)操作實例詳解_python
- 2022-08-15 LCP創建bond接口
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支