vector of vector

closed account (y3091hU5)
hi.i wanna define a vector of vector that have not certain member.actually i have two variable that show the rows and the columns and a third one that i want put them back into the rows and columns.how do it?
Your question isn't clear. Can you rephrase it to be clearer?

To define a vector of vectors of integers:

std::vector<std::vector<int>> my_vec_of_vecs;

If the nesting of template arguments is confusing, you can define types to make it clearer:

1
2
3
using VecOfInts = std::vector<int>;

std::vector<VecOfInts> my_vec_of_vecs;
Last edited on
Similar topic here: http://www.cplusplus.com/forum/beginner/249206/

You want to define a vector of a vector. Check. That has no members. Check. You have two variables that have the number of rows and columns. Check.

1
2
	int no_of_columns = 5, no_of_rows = 5;
	vector < vector<int> > two_d_vector = vector<vector<int> >(no_of_columns, vector<int>(no_of_rows));

Or
1
2
3
	int no_of_columns = 5, no_of_rows = 5;
	vector < vector<int> > two_d_vector(no_of_rows);
	two_d_vector.resize(no_of_rows, vector<int>(no_of_columns));

Or
1
2
3
4
5
	int no_of_columns = 5, no_of_rows = 5;
	vector < vector<int> > two_d_vector(no_of_rows);
	
	for(size_t i = 0; i < two_d_vector.size(); i++)
		two_d_vector.resize(no_of_columns);

'third one that I want put them back into the rows and columns'. What? ;-;
So you have a third variable that you want to 'put back into rows and columns' what does that mean? .-.
Can you give an example or something?

Note that you should call the vector by its indexes and not with push_back() because we had resized the vector. Otherwise you would have 5x5 of 0s (in my snippet) and then whatever you pushed back.
Last edited on
closed account (y3091hU5)
dear Mikey boy and Grime
You're right. I made a mistake. It seems that what i tell is a 3D vector. Now again!
I wanna define a vector of vector with no certain member but your code have certain member. The rows and columns are known. I wanna the inner vector show the columns and outer vector show the rows that they have not certain member.
thanks.
Last edited on
My first snippet might have interchanged columns and rows. But aren't the other two what you want?

I don't get what you had meant otherwise. I thought I did. Maybe it will help us if you can post the actual question..
What do you mean by "no certain member"? The type that you store in a vector need to be specified.
closed account (y3091hU5)
may show me output of them?
may show me output of them?

What? What are you asking here?

I appreciate English may not be your first language, but can you try and be more clear in what you're asking for?

You can output the contents of a vector by iterating over each element of the vector, and outputing it.

Alternatively, you can use std::copy to copy the vector to std::cout .
Last edited on
closed account (y3091hU5)
I mean i cant run your code. And i cant see output. I wanna to you show me output of your code.
@badoom, no one has given you any code. You need to show us your code to understand better what you mean.
closed account (y3091hU5)
1
2
3
4
5
6
7
	int no_of_columns = 5, no_of_rows = 5;
	vector < vector<int> > two_d_vector(no_of_rows);
	
	for(size_t i = 0; i < two_d_vector.size(); i++)
		two_d_vector.resize(no_of_columns);

	


Yea, I mean for example in this code show me an example output. I mean with this way in a special case.
Last edited on
That snippet's wrong. two_d_vector.resize(no_of_columns); should've been two_d_vector[i].resize(no_of_columns); sorry I'm a typo king..

I don't know what kind of 'example' you were expecting. That snippet is just for reserving space and the vector is just a regular 2d vector, so you would call it like you would have with a regular vector itself.

Anyways you should be a little familiar with vectors http://www.cplusplus.com/reference/vector/vector/ and especially their methods. If you see a method like .resize() that you're not familiar with, then Google! Don't blindly copy-paste, ask yourself what a piece of code does.

As it turns out sometimes people replying can be wrong too, like I was with that post, but you should've been familiar enough that you could've spotted the error is what I'm saying.

Anyways here's an 'example' demonstrating how 'calling 2D vectors' works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int no_of_columns = 5, no_of_rows = 5;
	vector < vector<int> > two_d_vector(no_of_rows);

	cout << " Vector Created With 5 rows.";

	for (size_t i = 0; i < two_d_vector.size(); i++)
		two_d_vector[i].resize(no_of_columns);

	cout << "\n Vector Rows Resized To Have 5 Columns";

	for (size_t i = 0; i < two_d_vector.size(); i++)
		for (size_t j = 0; j < two_d_vector[i].size(); j++)
			two_d_vector[i][j] = 5;

	cout << "\nVector indexes filled with '5'";
	cout << "Here are vector Elements:\n";

	for (size_t i = 0; i < two_d_vector.size(); i++) {
		for (size_t j = 0; j < two_d_vector[i].size(); j++)
			cout << two_d_vector[i][j] << ' ';
		cout << '\n';
	}
	system("pause");
}
closed account (y3091hU5)
@ Grime unfortunately it was the first time that i used a vector and i confused! But this example is really what i need.
Thanks you for your help.
Topic archived. No new replies allowed.