網站首頁 編程語言 正文
用vector創建二維數組的幾種方法
方法一
#include <iostream>
#include <vector>
using namespace std;
?
void test01() {
? ? ?//創建一個外層容器
?? ?vector<vector<int>>v;
? ? //創建一些內層容器,并賦值
?? ?vector<int>v1(10,1);
?? ?vector<int>v2(10,2);
?? ?vector<int>v3(10,3);
? ? //將內層小容器插入到大容器之中,類似于二維數組。
?? ?v.push_back(v1);
?? ?v.push_back(v2);
?? ?v.push_back(v3);
}
遍歷訪問:
1.便于理解法
for (int i = 0; i < 3; i++) {
? ? ? ? for (int j = 0; j < 10; j++) {
? ? ? ? ? ? cout << v[i][j] << " ";
? ? ? ? }
? ? ? ? cout << endl;
? ? }
2.對邏輯能力稍微有點要求
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) {
? ? ? ? for (vector<int>::iterator it1 = (*it).begin(); it1 != (*it).end(); it1++) {
? ? ? ? ? ? cout << *it1 << " ";
? ? ? ? }
? ? ? ? cout << endl;
? ? }
方法二:一維擴充法?
#include <iostream>
#include <vector> //如果想用vector必須包含對應頭文件
using namespace std;
?
int main()
{
?? ?vector<vector<int> > vec(m); //這里m就是相當于二維數組的行數,必須寫,不然報錯
?? ?
?? ?//這里創建一個m*n的二維vector
?? ?for(int i = 0;i<m;i++)
?? ?{//這里是給內層vector定義大小。默認是0,這里n是個數,不是值
?
?? ??? ?vec[i].resize(n); ?//利用resize進行擴充
?? ?}
?
?
? ?//賦值,我嘗試了一下vec.[i].push_back(10)來為其賦值,不過失敗了,可能不可以這樣做。
? ?for (int i = 0; i < 3; i++) {
?? ??? ?for (int j = 0; j < 5; j++) {
?? ??? ??? ?vec[i][j] = j + 100;
?? ??? ?}
?? ?}
?? ?
?? ?system("pause");
?? ?return 0;
}
vector定義二維數組的輸入和輸出
1.方式一
#include <iostream>
using namespace std;
#include <vector>
int main()
{
?? ?vector<vector<int>> matrix = { {1,2,3},{4,5,6} };
?? ?for (int i = 0; i < matrix.size(); i++) {
?? ??? ?for (int j = 0; j < matrix[i].size(); j++) {
?? ??? ??? ?cout << matrix[i][j]<<" " ;
?? ??? ?}
?? ??? ?cout << endl;
?? ?}
?? ?system("pause");
?? ?return 0;
}
1.方式二
#include <iostream>
using namespace std;
#include <vector>
int main()
{
?? ?int r = 0, c = 0;
?? ?cout << "輸出行數 r: ";//規定二維數組行數
?? ?cin >> r;
?? ?cout << "輸入列數 c: ";//規定二維數組列數
?? ?cin >> c;
?? ?vector<vector<int>> array;//定義二維數組
?? ?vector<int>v;//定義一維數組
?? ?array.clear();//將二維數組清空,或初始化(初始化代碼很簡單,不會可以百度)//也可不用這一步
?? ?int temp = 0;
?? ?for (int i = 0; i < r; i++)//輸入r*c的二維數組
?? ?{
?? ??? ?cout << "開始輸入第 " << i + 1 << " 行 " << endl;
?? ??? ?v.clear();//子數組返回時要清除
?? ??? ?for (int j = 0; j < c; j++)
?? ??? ?{
?? ??? ??? ?cout << "輸入第 " << j + 1 << " 列中數字:";
?? ??? ??? ?cin >> temp;
?? ??? ??? ?v.push_back(temp);
?? ??? ?}
?? ??? ?array.push_back(v);
?? ?}
?? ?cout << "數組為:" << endl;
?? ?for (int i = 0; i < r; i++)//打印輸入的二維數組
?? ?{
?? ??? ?for (int j = 0; j < c; j++)
?? ??? ?{
?? ??? ??? ?cout << array[i][j] << " ";
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?system("pause");
?? ?return 0;
}
2.方式三
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
?? ?vector<vector<int>>c(2, vector<int>(6));
?? ?for (int i = 0; i < c.size(); i++)
?? ?{
?? ??? ?for (int j = 0; j < c[i].size(); j++)
?? ??? ?{
?? ??? ??? ?cout << c[i][j] << " ";
?? ??? ?}
?? ??? ?cout << "\n";
?? ?}
?? ?system("pause");
?? ?return 0;
}
原文鏈接:https://blog.csdn.net/hacker_zrq/article/details/112162049
相關推薦
- 2022-05-27 C++?超詳細梳理繼承的概念與使用_C 語言
- 2023-09-18 子組件向父組件傳值的4種方法
- 2022-06-25 Python+matplotlib繪制條形圖和直方圖_python
- 2022-07-30 分布式session的問題(使用SpringSession和redis解決)
- 2023-05-23 手把手教你如何一眼分辨是C還是C++_C 語言
- 2022-12-21 python中把嵌套的列表合并成一個列表方法總結_python
- 2022-07-02 搭建react-typescript-webpack開發環境
- 2022-06-11 Python?tkinter庫圖形繪制例子分享_python
- 最近更新
-
- 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同步修改后的遠程分支