C++ 初始化二维 std::vector
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17663186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing a two dimensional std::vector
提问by Ferenc Deak
So, I have the following:
所以,我有以下几点:
std::vector< std::vector <int> > fog;
and I am initializing it very naively like:
我非常天真地初始化它,例如:
for(int i=0; i<A_NUMBER; i++)
{
std::vector <int> fogRow;
for(int j=0; j<OTHER_NUMBER; j++)
{
fogRow.push_back( 0 );
}
fog.push_back(fogRow);
}
And it feels very wrong... Is there another way of initializing a vector like this?
而且感觉很不对……有没有其他的方法可以像这样初始化一个vector?
回答by hmjd
Use the std::vector::vector(count, value)
constructor that accepts an initial size and a default value:
使用std::vector::vector(count, value)
接受初始大小和默认值的构造函数:
std::vector<std::vector<int> > fog(
A_NUMBER,
std::vector<int>(OTHER_NUMBER)); // Defaults to zero initial value
If a value other than zero, say 4
for example, was required to be the default then:
4
例如,如果需要将非零值作为默认值,则:
std::vector<std::vector<int> > fog(
A_NUMBER,
std::vector<int>(OTHER_NUMBER, 4));
I should also mention uniform initialization was introduced in C++11, which permits the initialization of vector
, and other containers, using {}
:
我还应该提到在 C++11 中引入了统一初始化,它允许初始化vector
, 和其他容器,使用{}
:
std::vector<std::vector<int> > fog { { 1, 1, 1 },
{ 2, 2, 2 } };
回答by juanchopanza
There is no append
method in std::vector
, but if you want to make a vector containing A_NUMBER
vectors of int
, each of those containing other_number
zeros, then you can do this:
中没有append
方法std::vector
,但是如果您想制作一个包含 的A_NUMBER
向量的向量int
,每个向量都包含other_number
零,那么您可以这样做:
std::vector<std::vector<int>> fog(A_NUMBER, std::vector<int>(OTHER_NUMBER));
回答by Jason
Let's say you want to initialize 2D vector, m*n, with initial value to be 0
假设您要初始化二维向量 m*n,初始值为 0
we could do this
我们可以这样做
#include<iostream>
int main(){
int m = 2, n = 5;
vector<vector<int>> vec(m, vector<int> (n, 0));
return 0;
}
回答by sarthakgupta072
The general syntax, as depicted already is:
已经描述过的一般语法是:
std::vector<std::vector<int> > v (A_NUMBER, std::vector <int> (OTHER_NUMBER, DEFAULT_VALUE))
Here, the vector 'v' can be visualised as a two dimensional array, with 'A_NUMBER' of rows, with 'OTHER_NUMBER' of columns with their initial value set to 'DEFAULT_VALUE'.
在这里,向量“v”可以被可视化为一个二维数组,“A_NUMBER”行,“OTHER_NUMBER”列,初始值设置为“DEFAULT_VALUE”。
Also it can be written like this:
也可以这样写:
std::vector <int> line(OTHER_NUMBER, DEFAULT_VALUE)
std::vector<std::vector<int> > v(A_NUMBER, line)
Inputting values in a 2-D vector is similar to inputting values in a 2-D array:
在二维向量中输入值类似于在二维数组中输入值:
for(int i = 0; i < A_NUMBER; i++) {
for(int j = 0; j < OTHER_NUMBER; j++) {
std::cin >> v[i][j]
}
}
Examples have already been stated in other answers....!
其他答案中已经说明了示例....!
回答by Muhirwa Gabo Oreste
I think the easiest way to make it done is :
我认为最简单的方法是:
std::vector<std::vector<int>>v(10,std::vector<int>(11,100));
10 is the size of the outer or global vector, which is the main one, and 11 is the size of inner vector of type int, and initial values are initialized to 100! That's my first help on stack, i think it helps someone.
10是外层或全局向量的大小,主要是,11是int类型的内层向量的大小,初始值初始化为100!这是我在堆栈上的第一个帮助,我认为它可以帮助某人。
回答by Smit
The recommended approach is to use fill constructor to initialize a two-dimensional vector with a given default value :
推荐的方法是使用 fill 构造函数来初始化一个具有给定默认值的二维向量:
std::vector<std::vector<int>> fog(M, std::vector<int>(N, default_value));
where, M and N are dimensions for your 2D vector.
其中,M 和 N 是二维向量的维度。
回答by Shashwat Nema
Suppose you want to initialize a two dimensional integer vector with n rows and m column each having value 'VAL'
假设您要初始化一个二维整数向量,其中 n 行和 m 列的值分别为“ VAL”
Write it as
写成
std::vector<vector<int>> arr(n, vector<int>(m,VAL));
std::vector<vector<int>> arr(n, vector<int>(m,VAL));
This VAL can be a integer type variableor constantsuch as 100
此 VAL 可以是整数类型变量或常量,例如 100
回答by Ritik Kamboj
My c++ STL
code to initialise 5*3 2-D vector
with zero
我的c++ STL
代码初始化5*3 2-D vector
与zero
#include <iostream>
using namespace std;
#include <vector>
int main()
{// if we wnt to initialise a 2 D vector with 0;
vector<vector<int>> v1(5, vector<int>(3,0));
for(int i=0;i<v1.size();i++)
{
for(int j=0;j<v1[i].size();j++)
cout<<v1[i][j]<<" ";
cout<<endl;
}
}