日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stl向量_用户定义大小的C ++ STL中的2D向量

發布時間:2023/12/1 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl向量_用户定义大小的C ++ STL中的2D向量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

stl向量

C ++ STL中的2D矢量 (2D Vector in C++ STL)

In C++ STL, a 2D vector is a vector of vector.

在C ++ STL中,二維向量是向量的向量。

Syntax to declare a 2D vector:

聲明2D向量的語法:

vector<vector<T>> vector_name{ {elements}, {elements}, ...};

1) C++ STL code to declare and print a 2D Vector (with same number of elements)

1)使用C ++ STL代碼聲明和打印2D向量(具有相同數量的元素)

// C++ STL code to declare and print a 2D Vector #include <iostream> #include <vector> // for vectors using namespace std;int main() {// Initializing 2D vector "v1" with// same number of vector elementsvector<vector<int> > v1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };// Printing the 2D vector's elementsfor (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v1[i].size(); j++)cout << v1[i][j] << " ";cout << endl;}return 0; }

Output

輸出量

1 2 3 4 5 6 7 8 9

2) C++ STL code to declare and print a 2D Vector (with different number of elements)

2)使用C ++ STL代碼聲明和打印2D向量(具有不同數量的元素)

// C++ STL code to declare and print a 2D Vector #include <iostream> #include <vector> // for vectors using namespace std;int main() {// Initializing 2D vector "v1" with// different number of vector elementsvector<vector<int> > v1{ { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };// Printing the 2D vector's elementsfor (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v1[i].size(); j++)cout << v1[i][j] << " ";cout << endl;}return 0; }

Output

輸出量

1 2 3 4 5 6 7 8 9

3) C++ STL code to declare and print a 2D Vector (Numbers of rows, columns and elements input by the user)

3)使用C ++ STL代碼聲明和打印2D向量(用戶輸入的行數,列數和元素數)

// C++ STL code to declare and print a 2D Vector #include <iostream> #include <vector> // for vectors using namespace std;int main() {int row;int col;// Input rows & columnscout << "Enter number of rows: ";cin >> row;cout << "Enter number of columns: ";cin >> col;// Declaring 2D vector "v1" with// given number of rows and columns// and initialized with 0vector<vector<int> > v1(row, vector<int>(col, 0));// Input vector's elementsfor (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v1[i].size(); j++) {cout << "Enter element: ";cin >> v1[i][j];}}// Printing the 2D vector's elementscout << "2D vector elements..." << endl;for (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v1[i].size(); j++)cout << v1[i][j] << " ";cout << endl;}return 0; }

Output

輸出量

Enter number of rows: 3 Enter number of columns: 4 Enter element: 1 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 5 Enter element: 6 Enter element: 7 Enter element: 8 Enter element: 9 Enter element: 10 Enter element: 11 Enter element: 12 2D vector elements... 1 2 3 4 5 6 7 8 9 10 11 12

翻譯自: https://www.includehelp.com/stl/2d-vector-in-cpp-stl-with-user-defined-size.aspx

stl向量

總結

以上是生活随笔為你收集整理的stl向量_用户定义大小的C ++ STL中的2D向量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。