Using Vector to implement two-dimensional vectors

Source: Internet
Author: User

If each element of a vector is a vector, it is called a two-dimensional vector, for example

Vector <vector <int> VV (3, vector <int> (4); // here, spaces between two ">" are indispensable.

A two-dimensional vector VV is constructed, which contains three elements. Each element contains a vector of four int elements. The compiler calls the vector constructor twice to construct the object VV. The first call constructor constructs an unknown vector containing four zeros <int> object:

[0] [1] [2] [3]
0 0 0 0

The second call to the constructor initializes three elements of the constructor with the unknown vector as the initial value. The result is:

VV [0] [1] [2] [3]
[0] 0 0 0 0
[1] 0 0 0 0
[2] 0 0 0 0

VV [I] indicates a vector composed of elements in the I (I =, 2) rows. Vv. Size () is 3, VV [1]. Size () is 4.

The length of each element in a two-dimensional vector can be different, for example

vector<vector<int> >vv;for(int c = 1; c <= 3; c++)vv.push_back(vector<int>(c,0));

The code above generates a two-dimensional vector with different lengths. The first row has only one element, the second row has two, and the third row has three elements.


Example: Enter n integers and divide them by the selected integers (called modulus). The integers are grouped by the remainder.

# Include <iostream> # include <vector> using namespace STD; void classify (vector <int> & V, int mode, vector <int> & vv) {for (vector <int >:: const_iterator iter = v. begin (); iter! = V. end (); ITER ++) {int Index = (* ITER) % mode; // obtain the remainder VV [Index]. push_back (* ITER); // insert element} int main () {int mode; // mode CIN> mode; vector <int> VV (mode ); // defines a two-dimensional vector int N with mode rows; // n integers CIN> N; vector <int> V (n); // One-dimensional vector, save n integers for (INT I = 0; I <n; I ++) CIN> V [I]; // start classify (v, mode, vv ); for (I = 0; I <mode; I ++) {cout <"remainder is" <I <":"; if (VV [I]. empty () cout <Endl; else {for (vector <int>: const_iterator P = VV [I]. B Egin (); P! = VV [I]. End (); P ++) cout <* P <""; cout <Endl ;}} return 0 ;}

Running result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.