c++利用vector創(chuàng)建二維數組的幾種方法總結
更新時間:2022年11月09日 09:58:57 作者:OnlyForBetter
這篇文章主要介紹了c++利用vector創(chuàng)建二維數組的幾種方法總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
用vector創(chuàng)建二維數組的幾種方法
方法一
#include <iostream> #include <vector> using namespace std; ? void test01() { ? ? ?//創(chuàng)建一個外層容器 ?? ?vector<vector<int>>v; ? ? //創(chuàng)建一些內層容器,并賦值 ?? ?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就是相當于二維數組的行數,必須寫,不然報錯 ?? ? ?? ?//這里創(chuàng)建一個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: ";//規(guī)定二維數組行數 ?? ?cin >> r; ?? ?cout << "輸入列數 c: ";//規(guī)定二維數組列數 ?? ?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; }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- VS2019創(chuàng)建C++工程的的實現步驟
- VS2019創(chuàng)建c++動態(tài)鏈接庫dll與調用方法實踐
- Visual Studio 如何創(chuàng)建C/C++項目問題
- Visual Studio 2022 的安裝和創(chuàng)建C++項目(圖文教程)
- VS2019如何創(chuàng)建C++項目的實現示例
- 探索Visual C++下創(chuàng)建WPF項目的方法示例
- 如何使用visual studio2019創(chuàng)建簡單的MFC窗口(使用C++)
- Visual Studio 2019創(chuàng)建C++ Hello World項目的方法
- 在Visual Studio中用C++語言創(chuàng)建DLL動態(tài)鏈接庫圖文教程
- VC++創(chuàng)建msi文件的方法
- VC++6.0中創(chuàng)建C++項目的實現步驟
相關文章
劍指offer之C++語言實現鏈表(兩種刪除節(jié)點方式)
今天小編就為大家分享一篇關于劍指offer之C++語言實現鏈表(兩種刪除節(jié)點方式),小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02