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 92) 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 93) 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向量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF什么副职业比较赚钱?主要说下炼金
- 下一篇: 反转字符串中的元音字符_C程序消除字符串