Reserve size of vector<vector<float> >.

I'm having such a trouble:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
variables:
 int mRows, mColumns;
 mValues <vector<vector<float> >;

Matrix::Matrix(const unsigned int rows, const unsigned int columns) :
	mRows(rows), mColumns(columns)
{
	mValues.reserve(rows);
	{
		mValues.push_back(std::vector<float>);
	}

	for (unsigned int i = 0; i < mValues.size(); i++)
	{
		mValues[i].reserve(columns);
		for (unsigned int j = 0; j < columns; j++)
		{
			mValues[i][j].push_back(new std::vector<float>);
		}
	}
}

Unfortunately, I cannot reserve 'rows', error I get: in line 8 and 15 here:
1
2
3
passing ‘const std::vector<float>’ as ‘this’ argument of ‘void std::vector<_Tp, 
 _Alloc>::reserve(std::vector<_Tp, _Alloc>::size_type) [with _Tp = float, _Alloc = std::allocator<float>, 
 std::vector<_Tp, _Alloc>::size_type = long unsigned int]’ discards qualifiers [-fpermissive]


Is it possible to reserve a place for a vector of vectors? If so, how can I achieve it?
The calls to reserve are fine, but everything else is wrong.
What on earth is the following line supposed to mean?
mValues <vector<vector<float> >;

Instead of
mValues.push_back(std::vector<float>);
it has to be:
mValues.push_back(std::vector<float>());

mValues[i][j].push_back(new std::vector<float>);
makes no sense whatsoever. Correct:
mValues[i].push_back(0.0f);

You can save yourself all this trouble and write the following:
1
2
3
4
Matrix::Matrix(const unsigned int rows, const unsigned int columns) :
	mRows(rows), mColumns(columns), mValues(rows,std::vector<float>(columns))
{
}
Last edited on
Well, it means, that every place in a vector has its own vector as an element. To create a Matrix, you need to have 2 dimensional array (or like in my example - vector). Your answer is correct for only one vector, but I want to have a vector of vectors - like array of arrays, but more dynamic).
No. mValues(rows,std::vector<float>(columns)) initializes a vector of vectors of float.
You're right, absolutely right, i gave me quite a fresh look on a subject. Thank you.
Topic archived. No new replies allowed.