我用中文写个总结:
1.初试化方式
(1)
vector<vector<int>> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
(2)
#include<iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<string> > v;
vector<string> r,w,k;
for(int i=0;i<5;i++) r.push_back("a");
for(int i=0;i<5;i++) w.push_back("b");
for(int i=0;i<5;i++) k.push_back("c");
v.push_back(r);
v.push_back(w);
v.push_back(k);
for(int i=0;i<v.size();i++){
for(int j=0;j<v[i].size();j++){
cout<<v[i][j]<<" ";
}
cout<<endl;
}
}
2.遍历方式
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
3.值得注意的是:
vector<vector<int>> vect
We can divide this declaration to two parts, which will help us to understand the below concepts.
1. vect is a 2D vector consisting multiple elements of type vector.
2. vector is a 1D vector consisting of multiple int data.
So we can use iterator provided by STL instead of i,j variable used in for loop. It can reduce the error which can happen wrt to i, j operations(i++, j++)
In the below code we are using iterator to access the vector elements.
1. We are getting vect 1D vectors of type vector from the 2D vector vect.
2. We are getting int elements to x from the vector vect 1D vector.
以下为原文:
2D Vector In C++ With User Defined Size
Difficulty Level : Easy
Last Updated : 28 Jul, 2021
A 2D vector is a vector of the vector. Like 2D arrays, we can declare and assign values to a 2D vector!
Assuming you are familiar with a normal vector in C++, with the help of an example we demonstrate how a 2D vector differs from a normal vector below:
/* Vectors belong to a C++ library
called STL so we need to import
it first! */
#include <vector>
using namespace std;
int main()
{
/*
In the case of a normal vector we initialize it as:
1. vector<datatype> variable_name
Now in the case of a 2D vector all we do is create
a vector of datatype vector.
We simply replace "datatype" with "vector<int>":
1. vector<vector<int>> variable_name
That's literally it! We just created a 2D vector!
On line 23 below we declare an actual 2D vector
named "vect".
*/
vector<vector<int>> vect;
return 0;
}
In a 2D vector, every element is a vector.
/* C++ code to demonstrate a 2D vector
with elements(vectors) inside it. */
#include <iostream>
#include <vector>
using namespace std;
int main()
{
/*
Below we initialize a 2D vector
named "vect" on line 12 and then
we declare the values on
line 14, 15 and 16 respectively.
*/
vector<vector<int>> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
/*
Now we print the values that
we just declared on lines
14, 15 and 16 using a simple
nested for loop.
*/
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3
4 5 6
7 8 9
Another approach to access the vector elements:
/* C++ code to demonstrate a 2D vector
with elements(vectors) inside it. */
#include <iostream>
#include <vector>
using namespace std;
int main()
{
/*
Below we initialize a 2D vector
named "vect" on line 12 and then
we declare the values on
line 14, 15 and 16 respectively.
*/
vector<vector<int>> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
/*
Now we print the values that
we just declared on lines
14, 15 and 16 using a simple
nested for loop with the help of iterator.
*/
/*
vector<vector<int>> vect
We can divide this declaration to two parts, which will
help us to understand the below concepts.
1. vect is a 2D vector consisting multiple elements of type vector<int>.
2. vector<int> is a 1D vector consisting of multiple int data.
So we can use iterator provided by STL instead of
i,j variable used in for loop. It can reduce the error which can
happen wrt to i, j operations(i++, j++)
In the below code we are using iterator to access the vector elements.
1. We are getting vect1D vectors of type vector<int> from the 2D vector vect.
2. We are getting int elements to x from the vector<int> vect 1D vector.
*/
for (vector<int> vect1D : vect)
{
for (int x : vect1D)
{
cout << x << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3
4 5 6
7 8 9
Like Java’s jagged arrays, each element of a 2D vector can contain a different number of values.
/*
C++ program to demonstrate a 2D vector where
each of its elements is of different size.
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
/*
We initialize a 2D vector
named "vect" on line 16 with
different number of values
in each element.
*/
vector<vector<int>> vect
{
/* Element one with 2 values in it. */
{1, 2},
/* Element two with 3 values in it. */
{4, 5, 6},
/* Element three with 4 values in it. */
{7, 8, 9, 10}
};
/*
Now we print the vector that we
just defined using a simple
nested for loop.
*/
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2
4 5 6
7 8 9 10
We hope you that you leave this article with a better understanding of 2D vectors and are now confident enough to apply them on your own.
This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.