Я создаю 2D-вектор (0,0) и хочу изменить его размер (n, m), однако моя функция изменения размера должна оставаться const
Я пробовал сделать
void resize(int row, int col) const
{
array.resize(row, vector<int>(col));
}
но продолжайте получать
passing ‘const std::vector<std::vector<int>, std::allocator<std::vector<int> > >’ as ‘this’ argument discards qualifiers
Как мне это сделать?
Matrix.h
#pragma once
#include <vector>
using namespace std;
template <typename Object>
class matrix
{
public:
matrix(int rows, int cols) : array{ rows } {
for (auto& thisRow : array)
thisRow.resize(cols);
}
matrix( initializer_list<vector<Object>> lst ) : array( lst.size( ) )
{
int i = 0;
for( auto & v : lst )
array[ i++ ] = std::move( v );
}
matrix( const vector<vector<Object>> & v ) : array{ v } {}
matrix( vector<vector<Object>> && v ) : array{ std::move( v ) } {}
matrix() {}
const vector<Object> & operator[]( int row ) const
{
return array[ row ];
}
vector<Object> & operator[]( int row )
{
return array[ row ];
}
int numrows() const
{
return array.size( );
}
int numcols() const
{
return numrows( ) ? array[ 0 ].size( ) : 0;
}
void resize(int row, int col) const
{
array.resize(row, vector<int>(col));
}
private:
vector<vector<Object>> array;
};
main . cpp
matrix<int> mat = matrix<int>();
cout << "Zero-parameter matrix (rows,cols) = (" << mat.numrows() << "," << mat.numcols() << ")" << endl;
mat.resize(4, 3);
cout << "Resized matrix to 4x3" << endl;
cout << mat << endl;
mat[2][1] = 12;
cout << "Modified (2,1)" << endl;
cout << mat << endl;