I am trying to create a simple 2D array with the size being the parameters passed by the method. In C# we would have something like this:
float[,] GenerateNoiseMap(int mapWidth, int mapHeight){
float noiseMap[,] = new float[mapWidth, mapHeight];
return noiseMap;
}
Any idea how I can create a 2D array and return it in C++?
So far the compiler gives me an error that the size has to be a constant value, and I want the values to be what the method parameters are
I like a simple wrapper around a 1D vector:
#include <vector>
class Matrix
{
private:
size_t rows, columns;
std::vector<double> matrix;
public:
Matrix(size_t numrows, size_t numcols) :
rows(numrows), columns(numcols), matrix(rows * columns)
{
}
double & operator()(size_t row, size_t column)
{
return matrix[row * columns + column]; // note 2D coordinates are flattened to 1D
}
double operator()(size_t row, size_t column) const
{
return matrix[row * columns + column];
}
size_t getRows() const
{
return rows;
}
size_t getColumns() const
{
return columns;
}
};
Documentation for std::vector
Usage:
Matrix noiseMap(mapWidth, mapHeight);
You cannot have a 2D C-like array with a dynamic size on both dimensions in C++. C# arrays look a bit like them, but they're not the same thing at all.
I recommend boost::multi_array for this:
#include <boost/multi_array.hpp>
boost::multi_array<float, 2> GenerateNoiseMap(int mapWidth, int mapHeight) {
return boost::multi_array<float, 2>{boost::extents[mapWidth][mapHeight]};
}
float ** create_matrix(size_t m, size_t n)
{
float ** answer = new float*[m];
for (size_t i =0; i < m; i++)
answer[i] = new float[n];
return answer;
}
Related
Arrays have this nice property of being contiguous blocks of memory. When using new to allocate memory for an array, it returns a pointer to a contiguous block of memory. However, if I allocate a matrix using new, like this:
#include <iostream> //std::cin
int main()
{
int n, m;
std::cin >> n >> m;
int** mat = new int*[n];
for (int i = 0; i < n; i++)
mat[i] = new int[m];
//use the matrix in some way
for (int i = 0; i < n; i++)
delete[] mat[i];
delete[] mat;
return 0;
}
This works, but mat doesn't point to a contiguous block of size n * m * sizeof(int). How can I do this in C++? I am just complying to the latest standard (that is C++17) and nothing else. I want an answer that doesn't involve STL containers or external libraries.
Please don't answer about C, as that is pretty easy to do in both C99 and C11 using variable-length arrays:
#include <stdio.h> //scanf
#include <stdlib.h> //malloc, free
int main()
{
int n, m;
scanf("%d %d", &n, &m);
//int mat[n][m]; VLA, but I want dynamic
int (*mat)[m] = malloc(n * sizeof *mat);
//use the matrix in some way;
free(mat);
return 0;
}
Here's what you were doing, almost exactly the same but without the non-contiguous memory:
#include <iostream> //std::cin
#include <memory>
int main()
{
int n, m;
std::cin >> n >> m;
auto matrix_data = std::make_unique<int[]>(n * m);
auto mat = std::make_unique<int[]>(n);
for(int i = 0; i < n; i++) { mat[i] = matrix_data.get() + i * m; }
// Use the matrix in some way
// No need to free anything - we're using smart pointers.
// No need to return 0 from main - that's the default
}
Notes:
This is still ugly code... you'd likely better create a proper matrix class, or better still - use somebody else's implementation.
It is better to follow #Someprogrammerdude's suggestion and use arithmetic rather than an array of pointers.
I kind of still don't know what particularly are you asking for. To store matrix elements in a contiguous location, simply allocate the memory for them as a one-dimensional dynamic array. The two basic options have been already discussed, either use a vector:
std::vector<int> mat(m * n);
or, if its memory overhead is significant for you, use a unique pointer:
auto mat = std::make_unique<int[]>(m * n);
Then, to access an element with i row index and j column index, simply use:
a_i_j = mat[i * n + j];
assuming m is the number of rows and n is the number of columns. This formula stores elements in so-called row-major order. If you need the column-major order, switch to:
a_i_j = mat[j * m + i];
Of course, whole approach would be much better encapsulated in a class with some getter operator mat(i, j);.
Here is a 2D matrix class that enables m[i][j] support and has a contiguous storage. It contains a minimalistic set of member functions to create and access elements.
It's a shame that the STD library does not provide such functionality. [i][j] element access is way less error prone than index arithmetics.
#include <cstddef>
#include <vector>
template <typename T>
class matrix
{
public:
using value_type = T;
private:
class row_view
{
public:
constexpr row_view(size_t length, value_type *begin)
: _length(length),
_begin(begin)
{}
value_type &operator[](size_t i) {
// TODO: check bounds
return *std::next(_begin, i);
}
const value_type &operator[](size_t i) const {
// TODO: check bounds
return *std::next(_begin, i);
}
size_t size() const {
return _length;
}
private:
size_t _length;
value_type *_begin;
};
public:
matrix(size_t rows, size_t cols, value_type &&defaultVal)
: _rows(rows),
_array(rows * cols, std::move(defaultVal))
{}
matrix(size_t rows, size_t cols)
: matrix(rows, cols, value_type{})
{}
size_t rows() const noexcept {
return _rows;
}
size_t cols() const noexcept {
return _array.size() / _rows;
}
auto operator[](size_t rowIndex) -> row_view{
const size_t offset = cols() * rowIndex;
return {cols(), &_array[offset]};
}
auto operator[](size_t rowIndex) const -> const row_view{
const size_t offset = cols() * rowIndex;
return {cols(), &_array[offset]};
}
private:
size_t _rows;
std::vector<value_type> _array;
};
https://godbolt.org/z/qoG6jGbh5
I'm trying to create a template to allocate dynamically an 2D matrix.
Usually what I do is:
float **Allocate_matrix_float (int m, int n)
{
float **v;
int i;
if (m < 1 || n < 1) {
printf ("** Invalid parameter **\n");
return (NULL);
}
v = (float **) calloc (m, sizeof(float *));
if (v == NULL) {
printf ("** Unsufficient memory **");
return (NULL);
}
for ( i = 0; i < m; i++ ) {
v[i] = (float*) calloc (n, sizeof(float));
if (v[i] == NULL) {
printf ("** Unsufficient memory **");
return (NULL);
}
}
return (v);
}
float **free_matrix_float (int m, int n, float **v)
{
int i;
if (v == NULL) return (NULL);
if (m < 1 || n < 1) {
printf ("** invalid parameter**\n");
return (v);
}
for (i=0; i<m; i++) free (v[i]);
free (v);
return (NULL);
}
However I'd like to create a template to allocate any type of 2D matrix. Can anyone help me?
The ideal would be something like:
template<typename T>
T**Allocate_matrix(int n, int m)
...
All of your allocation and deallocation code can be replaced by
std::vector<std::vector<float>> matrix(m, std::vector(n));
Seriously. It even deallocates itself when it goes out of scope, so you have almost no memory management issues.
#include <iostream>
#include <vector>
int main()
{
size_t m;
size_t n;
std::cin >> m >> n;
// floats initialized to 0.0
std::vector<std::vector<float>> fltmatrix(m, std::vector<float>(n));
// doubles initialized to 0.0
std::vector<std::vector<double>> dblmatrix(m, std::vector<double>(n));
// bools initialized to true
std::vector<std::vector<bool>> boolmatrix(m, std::vector<bool>(n), true);
// ints initialized to 42
std::vector<std::vector<int>> intmatrix(m, std::vector<int>(n, 42));
} <-- all vectors are released here.
Practically no effort required.
However, because each vector is it's own independent entity and you have m+1 vectors, you have m+1 different places in memory that your program needs to look to return a value. This can have a really bad impact on your programs performance as small matrices, say a 3x3, can't take full advantage of the CPU's caching and this can be very, very noticeable when crunching large numbers of matrices. If you don't care, stop reading and go with the simple vector of vector approach.
If you do care, wrap a 1D vector in a class:
#include <iostream>
#include <vector>
template<class TYPE>
class Matrix
{
private:
size_t rows, columns;
std::vector<TYPE> matrix;
public:
Matrix(size_t numrows, size_t numcols) :
rows(numrows), columns(numcols), matrix(rows * columns)
{
}
Matrix(size_t numrows, size_t numcols, TYPE init) :
rows(numrows), columns(numcols), matrix(rows * columns, init)
{
}
TYPE & operator()(size_t row, size_t column)
{
// check bounds here
return matrix[row * columns + column];
}
TYPE operator()(size_t row, size_t column) const
{
// check bounds here
return matrix[row * columns + column];
}
size_t getRows() const
{
return rows;
}
size_t getColumns() const
{
return columns;
}
friend std::ostream & operator<<(std::ostream & out, const Matrix & in)
{
for (int i = 0; i < in.getRows(); i++)
{
for (int j = 0; j < in.getColumns(); j++)
{
out << in(i, j) << ' ';
}
out << std::endl;
}
return out;
}
};
int main()
{
size_t m;
size_t n;
std::cin >> m >> n;
// floats initialized to 0.0
Matrix<float> fltmatrix(m, n);
std::cout << fltmatrix << std::endl;
// doubles initialized to 0.0
Matrix<double> dblmatrix(m, n);
std::cout << dblmatrix << std::endl;
// bools initialized to true
Matrix<bool> boolmatrix(m, n, true);
std::cout << boolmatrix << std::endl;
// ints initialized to 42
Matrix<int> intmatrix(m, n, 42);
std::cout << intmatrix << std::endl;
}
More effort, but should be faster. Profile your program to see if Matrix is right for you.
operator<< is included as an output convenience and as an example of how to access Matrix's cells.
And if you just have to use an array... Things get a lot uglier. For one thing, you will have to be Rule of Three (and possibly Rule of Five) compliant and pick up a bunch of extra functions that, frankly, you're probably not going to get right the first few times.
I'm not even sure I can get it right if I just bang one out, and I have a perfectly good alternative, so I'm not going to. What you can gain is a matrix that does not spend time initializing the matrix before use. If that is an issue (profile, profile, profile!) I'd call that a new question. The current question uses calloc so it doesn't look like OP is concerned.
As paddy mentions here using vector-of-vector is not practical, difficult to change and suffers from cache misses. As well as using bare pointers is impractical in terms of C++ which provides better tools like operator overloading.
Taking paddy's implementation as basis your 2d matrix can be implemented in the following way:
template <class T>
class SimpleMatrix
{
public:
SimpleMatrix( int rows, int cols, const T& initVal = T() )
: m_data( rows * cols, initVal )
, m_rows( rows )
, m_cols( cols )
{
}
// Direct vector access and indexing
operator const vector<T>& () const { return m_data; }
int Index( int row, int col ) const { return row * m_cols + col; }
// Get a single value
T & Value( int row, int col ) { return m_data[Index(row,col)]; }
const T & Value( int row, int col ) const { return m_data[Index(row,col)]; }
// Proxy structure to allow [][] indexing
struct Proxy
{
private:
friend class SimpleMatrix<T>;
SimpleMatrix<T>* m_matrix;
int m_row;
Proxy( SimpleMatrix<T>* m, int row ) : m_matrix(m), m_row(row) {}
public:
T & operator[] ( int col ) { return m_matrix->Value(m_row, col); }
const T & operator[] ( int col ) const { return m_matrix->Value(m_row, col); }
};
Proxy operator[]( int row ) { return Proxy(this, row); }
const Proxy operator[]( int row ) const { return Proxy(const_cast<SimpleMatrix<T>*>(this), row); }
private:
vector<T> m_data;
int m_rows;
int m_cols;
};
And use it in the following way:
SimpleMatrix<int> m(10, 2);
const SimpleMatrix<int>& cm = m;
m[1][1] = 1;
cout << cm[1][1];
This will also allow you to check the boundaries of the index.
For a vector in C++, I have
class Vec
{
public:
int len;
double * vdata;
Vec();
Vec(Vec const & v)
{
cout<<"Vec copy constructor\n";
len = v.len;
vdata=new double[len];
for (int i=0;i<len;i++) vdata[i]=v.vdata[i];
};
I would greatly appreciate it if you could help me how to write an analogous code for a matrix. I am thinking something like this:
class Mat
{
public:
int nrows;
int ncols;
double * mdata;
Mat();
Mat(Mat const & m)
{
cout<<"Mat copy constructor\n";
nrows = m.nrows;
ncols = m.ncols;
But I don't know how to code the memory allocation for a matrix using the idea that first we put all the elements into a 1D array (row1 row2 ... rown) then we chop the array into rows and then chop each row into columns. Particularly, could you help me translate this idea into C++ language that is analogous to the following:
vdata=new double[len];
for (int i=0;i<len;i++) vdata[i]=v.vdata[i];
};
I am thinking of something like this:
double *data=new double[nrows*ncols];
for (int i=0;i<nrows;i++)
{
for (int j=0;j<ncols,j++){data(i,j)=m.mdata[i][j]};
};
But I am not sure about this part:
data(i,j)=m.mdata[i][j]
Also, I am supposed to use a pure virtual element indexing method: the (i,j) element of a Mat object m will be retrieved by m(i,j). I have to provide both const and non-const versions of this indexing operator.<-- May you show me how I could do this?
Thanks a lot.
Use as a single-dimensional array. You will notice that in practice, it's generally much simpler to use a 1d-array for such things.
class Matrix
{
public:
Matrix(unsigned int rows, unsigned int cols)
: _rows(rows)
, _cols(cols)
, _size(_rows*_cols)
, _components(new double[_size])
{
for(unsigned int i = 0; i < _size; ++i)
{
_components[i] = 0;
}
}
~Matrix()
{
delete[] _components;
}
double& operator()(unsigned int row, unsigned int col)
{
unsigned int index = row * _cols + col;
return _components[index];
}
private:
unsigned int _rows;
unsigned int _cols;
unsigned int _size;
double* _components;
};
However, if you want to actually use matrices and vectors, and not just implement them for learning, I would really advise you to use the Eigen library. It's free and open source and has great and easy-to-use vector and matrix classes.
While Eigen is great to use, if you want to look at source code of an existing implementation, it can be quite confusing for new programmers - it's very general and contains a lot of optimizations. A less complicated implementation of basic matrix and vector classes can be found in vmmlib.
Also you can use one standard vector to implement matrix but vector size will be nrows * ncols:
#include <vector>
class Mat {
public:
Mat(int rows, int cols):
nrows(rows),
ncols(cols),
elems(rows*cols,0)
{}
Mat(const Mat &m):
nrows(m.nrows),
ncols(m.ncols),
elems(m.elems.begin(), m.elems.end())
{}
double celem(int i,int j) const {
return elems[ncols*i + nrows*j];
}
double *pelem(int i,int j) {
return &elems[ncols*i + nrows*j];
}
private:
int nrows;
int ncols;
vector<double> elems;
};
I am trying to calculate the transpose of a matrix that is not n by n.
The problem is I have to allocate new memory to each element being added, I do not have to delete the **array.
Code is something like that.
// initialize a 2-D array.
array = new int*[row];
for (int i=0; i<row; i++){
arr[i] = new int[col]();
}
Now I am considering just One case that suppose my matrix is 3*4. transpose of Matrix has dim 4*3.
I do the following code But it is giving "SEGMENTATION FAULT". The idea is i allocate a new memory to the element that would be added as a result of transpose.
Code is:
int r=col;
int c=row;
if (col<c){
arr = new int*[row];
for (int i=col; i<=c; i++){
arr[i] = new int[col](); // trying to allocate New Memory to elem.
}
It is giving error here.
Any Help. Also if there is any other method to solve this prolem , do suggest.
In your second code sample, you write over the array limits. arr is row elements long, counting from 0 to row - 1. In the for loop your index i goes from col to c which is equivalent to row and one element beyond the array. Correct code would be < instead of <=
for (int i=col; i < c; i++){
arr[i] = new int[col](); // trying to allocate New Memory to elem.
}
Apart from that, may I suggest you look at Wikipedia: Transpose, because in your second case you can use the the first code sample with just row and col switched.
Write a wrapper with accessor functions (e.g. operator(row,col) for matrices), and use a single-dimensional array of size rows*cols internally.
It makes things much easier, and keeps the data for this matrix together. This can have cache benefits for smaller matrices.
Here is an example, as requested in your comment. It's held very simple by purpose and does not use any templates. It uses a vector as internal storage. You can access the matrix elements using operator(..), e.g.
Matrix A(3,4);
// fill matrix
// you can access each element in a natural syntax as expected from math
int x = A(2,2);
A(2,2) = 3;
In addition, you probably should use exceptions instead of asserts to check for index overflow.
// matrix.h
#include <vector>
class Matrix
{
public:
Matrix(size_t rows, size_t cols);
int& operator()(size_t row, size_t col);
const int& operator()(size_t row, size_t col) const;
int& operator[](size_t index);
const int& operator[](size_t index) const;
Matrix get_transposed() const;
size_t compute_index(size_t row, size_t col) const;
void print();
private:
size_t _rows;
size_t _cols;
std::vector<int> _data;
}; // class Matrix
// matrix.cpp
#include "matrix.h"
#include <iostream>
#include <cassert>
Matrix::Matrix(size_t rows, size_t cols)
: _rows(rows)
, _cols(cols)
, _data(rows*cols, 0)
{
}
int& Matrix::operator()(size_t row, size_t col)
{
const size_t index = compute_index(row, col);
assert(index < _data.size());
return _data[index];
}
const int& Matrix::operator()(size_t row, size_t col) const
{
const size_t index = compute_index(row, col);
assert(index < _data.size());
return _data[index];
}
int& Matrix::operator[](size_t index)
{
return _data[index];
}
const int& Matrix::operator[](size_t index) const
{
return _data[index];
}
size_t
Matrix::compute_index(size_t row, size_t col) const
{
// here you should check that:
// row < rows
// col < cols
// and throw an exception if it's not
assert(row<_rows);
assert(col<_cols);
return row * _cols + col;
}
Matrix
Matrix::get_transposed() const
{
Matrix t(_cols,_rows);
for(size_t row = 0; row < _rows; ++row)
for(size_t col = 0; col < _cols; ++col)
{
t(col,row) = (*this)(row,col);
}
return t;
}
I need to create 2d array in c++.
I can't do it by int mas= new int[x][y]; or auto mas= new int[x][y];
I need to create an array dynamically like:
int x,y
auto mas= new int[x][y];//error - must be const.
Please help me.
int x,y;
x =3;
y = 5;
int ** mas = new int*[x];
for (int i=0;i<x;i++)
{
mas[i] = new int[y];
}
I think something like this.
Don't forget
for(int i=0;i<x;i++)
delete[] mas[i];
delete[] mas;
at the end.
The C++ tool for creating dynamically sized arrays is named std::vector. Vector is however one-dimensional, so to create a matrix a solution is to create a vector of vectors.
std::vector< std::vector<int> > mas(y, std::vector<int>(x));
It's not the most efficient solution because you pay for the ability to have each row of a different size. You you don't want to pay for this "feature" you have to write your own bidimensional matrix object. For example...
template<typename T>
struct Matrix
{
int rows, cols;
std::vector<T> data;
Matrix(int rows, int cols)
: rows(rows), cols(cols), data(rows*cols)
{ }
T& operator()(int row, int col)
{
return data[row*cols + col];
}
T operator()(int row, int col) const
{
return data[row*cols + col];
}
};
Then you can use it with
Matrix<int> mat(y, x);
for (int i=0; i<mat.rows; i++)
for (int j=0; j<mat.cols; j++)
mat(i, j) = (i == j) ? 1 : 0;
My advice would be to avoid the pain of multidimensional arrays in the first place and use a struct.
struct Point {
int x;
int y;
}
int points = 10;
Point myArray[points];
Then to access a value:
printf("x: %d, y: %d", myArray[2].x, myArray[2].y);
Depends on exactly what you're trying to achieve, though.
You can do the manipulations yourself.
int* mas = new int[x*y];
and access [i,j] by:
mas[i*y + j] = someInt;
otherInt = mas[i*y +j];
std::vector<std::vector<int> > mas(y, std::vector<int>(x));