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向量初始化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544831.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EasyUI-右键菜单变灰不可用效果

使用过EasyUI的朋友想必都知道疯狂秀才写的后台界面吧&#xff0c;作为一个初学者我不敢妄自评论它的好坏&#xff0c;不过它确实给我们提供了一个很好框架&#xff0c;只要在它的基础上进行修改&#xff0c;基本上都可以满足我们开发的需要。 知道“疯狂秀才”写的后台界面已经…

Oracle中insert into select和select into的区别

文章转自&#xff1a;http://www.linuxidc.com/Linux/2012-09/70984.htm 在Oracle中&#xff0c;将一张表的数据复制到另外一个对象中。通常会有这两种方法&#xff1a;insert into select 和 select into from。 前者可以将select 出来的N行(0到任意数)结果集复制一个新表中…

ThreadLocal中的3个大坑,内存泄露都是小儿科!

我在参加Code Review的时候不止一次听到有同学说&#xff1a;我写的这个上下文工具没问题&#xff0c;在线上跑了好久了。其实这种想法是有问题的&#xff0c;ThreadLocal写错难&#xff0c;但是用错就很容易&#xff0c;本文将会详细总结ThreadLocal容易用错的三个坑&#xff…

java中为按钮添加图片_如何在Java中为字符串添加双引号?

java中为按钮添加图片In Java, everything written in double-quotes is considered a string and the text written in double-quotes is display as it is. 在Java中&#xff0c; 双引号中的所有内容均视为字符串&#xff0c;而双引号中的文本按原样显示。 Suppose, if we wa…

基于.Net的单点登录(SSO)解决方案

为什么80%的码农都做不了架构师&#xff1f;>>> 前些天一位朋友要我帮忙做一单点登录&#xff0c;其实这个概念早已耳熟能详&#xff0c;但实际应用很少&#xff0c;难得最近轻闲&#xff0c;于是决定通过本文来详细描述一个SSO解决方案&#xff0c;希望对 大家有所…

Ajax在请求数据时显示等待动画遮罩

/*** 等待提醒 开始 *********************************************************************************************** -yzy -20150408 说明&#xff1a;在Ajax请求数据的时候&#xff0c;显示等待界面*/// 1、这里显示等待框$(#YWaitDialog).show();// 2、这里进行ajax的请…

c语言 函数的参数传递示例_isgreaterequal()函数以及C ++中的示例

c语言 函数的参数传递示例C isgreaterequal()函数 (C isgreaterequal() function) isgreaterequal() function is a library function of cmath header, it is used to check whether the given first value is greater than or equal to the second value. It accepts two va…

在android中ScrollView嵌套ScrollView解决方案

文章转载自&#xff1a;http://www.jb51.net/article/33054.htm大家好&#xff0c;众所周知&#xff0c;android里两个相同方向的ScrollView是不能嵌套的&#xff0c;那要是有这样的需求怎么办,接下来为您介绍解决方法&#xff0c;感兴趣的朋友可以了解下大家好&#xff0c;众所…

Cucumber 入门一

&#xff08;转自&#xff1a;http://www.cnblogs.com/jarodzz/archive/2012/07/02/2573014.html&#xff09; 第一次看到Cucumber和BDD&#xff08;Behavior Driven Development, 行为驱动开发&#xff09;&#xff0c;是在四年前。那时才開始工作&#xff0c;对软件測试工具相…

Python datetime astimezone()方法与示例

Python datetime.astimezone()方法 (Python datetime.astimezone() Method) datetime.astimezone() method is used to manipulate objects of datetime class of module datetime. datetime.astimezone()方法用于操作模块datetime的datetime类的对象。 It uses an instance …

Java中这7个方法,一不小心就用错了!

最近我们通过sonar静态代码检测&#xff0c;同时配合人工代码review&#xff0c;发现了项目中很多代码问题。除了常规的bug和安全漏洞之外&#xff0c;还有几处方法用法错误&#xff0c;引起了我极大的兴趣。我为什么会对这几个方法这么感兴趣呢&#xff1f;因为它们极具迷惑性…

java 标志一个方法为过时方法

使用 Deprecated 来标记方法 Deprecated//用来判断ip是否合法public boolean checkIp(String tempIp) {String regex "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3}"; // String regex2 "([1-9]|[1-9]\\\\d…

STL之顺序容器

顺序容器&#xff1a; vector&#xff1a;数组 list:链表 deque&#xff1a;双端数组 顺序容器适配器&#xff1a; stack&#xff1a;堆栈 queue&#xff1a;队列 priority_queue&#xff1a;优先级队列 deque是一个动态数组  deque与vector非常类似&#xff1b;  deque可以…

Java StringBuilder reverse()方法与示例

StringBuilder类reverse()方法 (StringBuilder Class reverse() method) reverse() method is available in java.lang package. reverse()方法在java.lang包中可用。 reverse() method is used to reverse this character sequence by the reverse of the sequence. reverse()…

这样设置,让你的 IDEA 好看到爆炸

今天这期我们来分享几个美化 IDEA 设置技巧&#xff0c;让你的 IDEA 与众不同。首先我们来看下 IDEA 默认设置&#xff0c;虽然不丑&#xff0c;但就是太单调&#xff0c;千篇一律。默认主题接着&#xff0c;我们来看下美化以后的界面&#xff0c;总体看起来是不是比默认好看了…

IMP-00002: 无法打开 D:\orcldat\test_20111024.dmp 进行读取,rman备份

文章转自&#xff1a;http://blog.csdn.net/wanglilin/article/details/6900633 首先&#xff0c;我的路径写错了&#xff0c;文件夹是orcldata我掉了个a。 其次&#xff0c;命令后添加 fully。 dos下随便哪个目录> [sql] view plaincopyprint? IMP username/pwddbname BU…

observable_Java Observable setChanged()方法与示例

observable可观察的类setChanged()方法 (Observable Class setChanged() method) setChanged() method is available in java.util package. setChanged()方法在java.util包中可用。 setChanged() method is used to set this Observable object status as changed. setChanged…

RabbitMQ 集群

2019独角兽企业重金招聘Python工程师标准>>> Clustering Guide A RabbitMQ broker is a logical grouping of one or several Erlang nodes, each running the RabbitMQ applicationand sharing users, virtual hosts, queues, exchanges, etc. Sometimes we refer …

使用uuid作为数据库主键,被技术总监怼了!

一、前言在日常开发中&#xff0c;数据库中主键id的生成方案&#xff0c;主要有三种数据库自增ID采用随机数生成不重复的ID采用jdk提供的uuid对于这三种方案&#xff0c;我发现在数据量少的情况下&#xff0c;没有特别的差异&#xff0c;但是当单表的数据量达到百万级以上时候&…

Android设置透明、半透明等效果

首先说明一点&#xff0c;关于透明的 Android 控件 background 问题&#xff0c;从转载来的文章看到最主要的一句有用的代码是&#xff1a; v.getBackground().setAlpha(100);//0~255透明度值 这里的 Alpha 值&#xff0c;实际上是 0-1 的取值范围。 以下内容转自&#xff…