具有使用者自定義大小的C++中的2D向量


向量的向量稱為2D向量。

演算法

Begin
   Declare a variable v to the 2D vector type.
   Initialize values to the vector v.
   Print “the 2D vector is:”.
   for (int i = 0; i < v.size(); i++)
      for (int j = 0; j < v[i].size(); j++)
         print the value of 2D vector v[i][j].
End.

示例

 現場演示

#include <iostream>
#include <vector> //header file for 2D vector in C++
using namespace std;
int main() {
   vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values.
   { 2, 7, 11 },
   { 3, 2, 1, 12 } };
   cout<<"the 2D vector is:"<<endl;
   for (int i = 0; i < v.size(); i++) { // printing the 2D vector.
      for (int j = 0; j < v[i].size(); j++)
      cout << v[i][j] << " ";
      cout << endl;
   }
   return 0;
}

輸出

the 2D vector is: 
4 5 3 10
2 7 11 
3 2 1 12

更新時間: 30-Jul-2019

2K+ 瀏覽量

開啟你的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.