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 initialized
    vector<vector<int> > two_D_vector;

    //below is an empty 1D vector
    vector<int> one_D_vector(5, 2);

    //pushing back the above 1D vector to the 
    //empty 2D vector each time
    for (int i = 0; i < 5; i++) {
        two_D_vector.push_back(one_D_vector);
    }

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (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 2

2)用用户定义的大小初始化向量 (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 defined
    int 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 size
    vector<vector<int> > two_D_vector(n, vector<int>(m));

    //by default all values are 0
    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (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 0

3)使用用户定义的大小和用户定义的元素进行初始化 (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 defined
    int n;
    
    cout << "Enter number of rows, n\n";
    cin >> n;

    //user defined 1D array
    vector<int> one_D_vector{ 1, 2, 3 };

    //2D vector initialized with user defined size,
    //user defined element
    vector<vector<int> > two_D_vector(n, one_D_vector);

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (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 3

Code 2:

代码2:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //n=no of rows which is user defined
    int n, m;
    
    cout << "Enter number of rows, n\n";
    cin >> n;

    //user defined 1D array
    cout << "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 element
    vector<vector<int> > two_D_vector(n, one_D_vector);

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (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 5

4)用用户定义的元素初始化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 elements
    vector<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 6

5)用其他向量的元素初始化向量 (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 only
    vector<vector<int> > two_D_vector{
        { 1, 2, 3 }, //comma separated lists
        { 5, 6, 7 },
        { 8, 9, 3 }
    };

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (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向量初始化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值