c++中int向量初始化_以不同的方式在C ++中初始化2D向量
c++中int向量初始化
Prerequisite: Initialize 1D vector
先決條件: 初始化一維向量
Before discussing about the initialization techniques let us state what a 2D vector is. A 2D vector in simple sense is a matrix having rows and column. In other words, a 2D vector is a vector of 1D vector, i.e., a vector having elements as 1D vector.
在討論初始化技術之前,讓我們先說明一下2D向量。 簡單來說,二維向量是具有行和列的矩陣。 換句話說,2D向量是1D向量的向量,即,具有元素作為1D向量的向量。
So what will be notation of 2D array?
那么2D數組的符號是什么?
vector<T> arr, where T is vector<W> where, W can be any datatype like int, char etc.
vector <T> arr ,其中T是vector <W> ,其中W可以是int,char等任何數據類型。
So a 2D integer vector we will define as vector<vector<int>> arr
所以我們將2D整數向量定義為vector <vector <int >> arr
Now let's get back to the point about initializing the 2D vector.
現在讓我們回到有關初始化2D向量的觀點。
1)初始化一個空的2D向量,然后迭代推回1D數組 (1) Initializing an empty 2D vector and then pushing back 1D arrays iteratively)
This is the most na?ve approach to initialize a 2D vector. Firstly, we just define an empty 2D vector. At that point, it has no idea about how many elements it's going to have. Then by using push_back() function we can simply keep adding 1D vectors at the back as per requirement. Now to add 1D vector we need to initialize that 1D arrays properly.
這是初始化2D向量的最幼稚的方法。 首先,我們只定義一個空的2D向量。 在那時,它尚不知道它將要包含多少個元素。 然后,通過使用push_back()函數,我們可以根據需要簡單地在背面添加一維向量。 現在要添加一維向量,我們需要正確初始化該一維數組。
Below is an example to add elements as per user wants.
以下是根據用戶需要添加元素的示例。
#include <bits/stdc++.h> using namespace std;int main() {//empty 2Dvector initializedvector<vector<int> > two_D_vector;//below is an empty 1D vectorvector<int> one_D_vector(5, 2);//pushing back the above 1D vector to the //empty 2D vector each timefor (int i = 0; i < 5; i++) {two_D_vector.push_back(one_D_vector);}//printing the 2D vectorcout << "printing the 2D vector\n";for (auto it : two_D_vector) {//it is now an 1D vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;}return 0; }Output:
輸出:
printing the 2D vector 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22)用用戶定義的大小初始化向量 (2) Initialize the vector with user defined size)
We can initialize the 2D vector with user-defined size also. It's quite similar like creating a 2D dynamic array using malloc() or new operator. So say we want to initialize a 2D vector to rows n, and column m, then we need to initialize an n size 2D vector with elements of m size 1D vector. We can do that just like below, by default all the valued in the 2D array gets initialized as 0.
我們也可以使用用戶定義的大小來初始化2D向量 。 就像使用malloc()或new運算符創建2D動態數組一樣。 假設要初始化第n行和第m列的2D向量,那么我們需要使用m個1D向量的元素初始化n個大小為2D的向量。 我們可以像下面那樣進行操作,默認情況下,二維數組中的所有值都初始化為0。
As we said earlier a 2D vector is a vector of a 1D vector. So for the upper use case, let's think exactly similarly as of 1D vector.
如前所述,二維向量是一維向量的向量。 因此,對于上層用例,讓我們與一維向量完全相似地思考。
So the outer vector has size n(number of rows)
因此,外部向量的大小為n(行數)
Let's define that,
讓我們定義一下
vector<T> arr(n);Now T is itself a 1D vector and has size m
現在T本身是一維向量,大小為m
Thus the element of the outer vector would vector<int>(m)
因此,外部向量的元素將為vector <int>(m)
This combining,
這種結合,
vector<vector<int>> arr(n, vector<int>(m)) #include <bits/stdc++.h> using namespace std;int main() {//n =no of rows//m =no of columns//both will be user definedint n, m;cout << "Enter number of rows, n\n";cin >> n;cout << "Enter number of columns, m\n";cin >> m;//2D vector initialized with user defined sizevector<vector<int> > two_D_vector(n, vector<int>(m));//by default all values are 0//printing the 2D vectorcout << "printing the 2D vector\n";for (auto it : two_D_vector) {//it is now an 1D vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;}return 0; }Output:
輸出:
Enter number of rows, n 6 Enter number of columns, m 3 printing the 2D vector 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 03)使用用戶定義的大小和用戶定義的元素進行初始化 (3) Initialize with user defined size and user defined element)
Here instead of initializing with default 0, we initialize with a user-defined value. The method will be similar to the method of a 1D vector.
在這里,不是使用默認0進行初始化,而是使用用戶定義的值進行初始化。 該方法將類似于一維矢量的方法。
So for the outer vector,
所以對于外部向量,
vector<T> arr(T,W)Where, W will be the user-defined element. Now here element is itself a 1D vector.
其中, W是用戶定義的元素。 現在這里的element本身就是一維向量。
Thus the example will be like below,
因此,示例如下所示,
Code 1:
代碼1:
#include <bits/stdc++.h> using namespace std;int main() {//n=no of rows which is user definedint n;cout << "Enter number of rows, n\n";cin >> n;//user defined 1D arrayvector<int> one_D_vector{ 1, 2, 3 };//2D vector initialized with user defined size,//user defined elementvector<vector<int> > two_D_vector(n, one_D_vector);//printing the 2D vectorcout << "printing the 2D vector\n";for (auto it : two_D_vector) {//it is now an 1D vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;}return 0; }Output:
輸出:
Enter number of rows, n 5 printing the 2D vector 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3Code 2:
代碼2:
#include <bits/stdc++.h> using namespace std;int main() {//n=no of rows which is user definedint n, m;cout << "Enter number of rows, n\n";cin >> n;//user defined 1D arraycout << "Define your 1D array which will be element\n";vector<int> one_D_vector;cout << "keep pushing numbers, press 0 to stop\n";cin >> m;while (m) {one_D_vector.push_back(m);cin >> m;}//2 Dvector initialized with user defined size,//user defined elementvector<vector<int> > two_D_vector(n, one_D_vector);//printing the 2D vectorcout << "printing the 2D vector\n";for (auto it : two_D_vector) {//it is now an 1D vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;}return 0; }Output:
輸出:
Define your 1D array which will be element keep pushing numbers, press 0 to stop 3 4 5 0 printing the 2D vector 3 4 5 3 4 5 3 4 5 3 4 5 3 4 54)用用戶定義的元素初始化2D向量 (4) Initialize the 2D vector with user defined elements)
We can also initialize the vector with user-defined elements. The syntax would be:
我們還可以使用用戶定義的元素初始化向量。 語法為:
vector<vector<int>> two_D_vector{comma separated 1D elements};The example is below:
示例如下:
#include <bits/stdc++.h> using namespace std;int main() {//initialize with user-defined elementsvector<int> arr{ 1, 2, 3, 4, 5, -1, -2, 6 };cout << "Printing the vector...\n";for (auto i : arr)cout << i << " ";cout << endl;return 0; }Output:
輸出:
Printing the vector... 1 2 3 4 5 -1 -2 65)用其他向量的元素初始化向量 (5) Initializing a vector with elements of other vector)
We can also initialize a vector using elements of another vector. The vector is passed as a constructor to initialize the new vector. This is a deep copy indeed.
我們還可以使用另一個向量的元素來初始化向量。 該向量作為構造函數傳遞,以初始化新向量。 這確實是一個深復制。
The example is like below:
示例如下:
#include <bits/stdc++.h> using namespace std;int main() {//2D vector initialized with user //defined -elements onlyvector<vector<int> > two_D_vector{{ 1, 2, 3 }, //comma separated lists{ 5, 6, 7 },{ 8, 9, 3 }};//printing the 2D vectorcout << "printing the 2D vector\n";for (auto it : two_D_vector) {//it is now an 1D vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;}return 0; }Output:
輸出:
printing the 2D vector 1 2 3 5 6 7 8 9 3翻譯自: https://www.includehelp.com/stl/initialize-2d-vector-in-cpp-in-different-ways.aspx
c++中int向量初始化
總結
以上是生活随笔為你收集整理的c++中int向量初始化_以不同的方式在C ++中初始化2D向量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析取值_圆锥曲线——高中解析几何全归纳
- 下一篇: c语言 函数的参数传递示例_isgrea