I am facing an issue with a 2D array of pointers. It compiles with no errors, however when I try to run the file, all I get is a single line saying I have a segmentation fault.
My header file:
#ifndef __TWODARRAY_H__
#define __TWODARRAY_H__
template <typename T>
class TwoDArray {
private:
T** theArray;
int numRows;
int numCols;
T defSpace;
public:
TwoDArray<T> (int r, int c, T def);
~TwoDArray<T>();
void insert(int r, int c, T value);
T access(int r, int c);
void remove(int r, int c);
void print();
int getNumRows();
int getNumCols();
};
#endif
My Methods:
#include "TwoDArray.h"
#include <iostream>
#include <assert.h>
#include <string>
//initializes the 2D Array
template <typename T>
TwoDArray<T>::TwoDArray(int r, int c, T def) {
assert(r > 0 && c > 0);
numRows = r;
numCols = c;
defSpace = def;
theArray = new T*[r];
for(int i=0; i<r; i++) {
theArray[i] = new T[c];
}
//sets all values to the default
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
theArray[i][j] = defSpace;
}
}
}
//deletes the 2D Array
template<typename T>
TwoDArray<T>::~TwoDArray() {
for(int i=0; i<numRows; i++) {
delete[] theArray[i];
}
delete[] theArray;
}
//inserts value v at row r and column c
template<typename T>
void TwoDArray<T>::insert(int r, int c, T value) {
assert(r < numRows && c < numCols);
assert(value != defSpace);
theArray[r][c] = value;
}
//get value at row r, column c
template<typename T>
T TwoDArray<T>::access(int r, int c) {
assert(r < numRows && c < numCols);
T result = theArray[r][c];
return result;
}
//set value at row r and column c back to default
template<typename T>
void TwoDArray<T>::remove(int r, int c) {
assert(r < numRows && c < numCols);
assert(theArray[r][c] != defSpace);
theArray[r][c] = defSpace;
}
//print the 2D Array
template<typename T>
void TwoDArray<T>::print() {
for(int i=0; i<numRows; i++) {
for(int j=0;j<numCols; i++) {
std::cout << theArray[i][j];
std::cout << " ";
}
std::cout << std::endl;
}
}
//gets number of rows for test
template<typename T>
int TwoDArray<T>::getNumRows() {
return numRows;
}
//gets number of columns for test
template<typename T>
int TwoDArray<T>::getNumCols() {
return numCols;
}
template class TwoDArray<int>;
template class TwoDArray<std::string>;
And my main:
#include <iostream>
#include <string>
#include "TwoDArray.h"
using std::cout;
using std::endl;
int main() {
TwoDArray<int>* i = new TwoDArray<int>(5, 5, 0);
TwoDArray<std::string>* s = new TwoDArray<std::string>(5, 5, "o");
i->insert(1, 1, 1);
i->insert(1, 3, 1);
i->insert(3, 2, 8);
i->insert(2, 0, 3);
i->insert(2, 4, 3);
i->insert(3, 2, 8);
i->print();
s->insert(0, 2, "North");
s->insert(4, 2, "South");
s->insert(2, 4, "East");
s->insert(2, 0, "West");
s->print();
return 0;
}
Any ideas why I'm getting a segmentation fault?
This is a mistake:
template<typename T>
void TwoDArray<T>::print() {
for(int i=0; i<numRows; i++) {
for(int j=0;j<numCols; i++) { // should be j++, not i++
std::cout << theArray[i][j];
std::cout << " ";
}
std::cout << std::endl;
}
}
as i is being incremented in both the outer and inner for and will eventually lead to i equalling numRows and going one past the end of the array, which is undefined behaviour and a possible cause of the segmentation fault.
As there is a dynamically allocated member in TwoDArray you need to prevent copying of instances of TwoDArray or implement the assignment operator and copy constructor (see What is The Rule of Three?).
If this is not a learning exercise you could use a vector<vector<T>> instead.
Also, as the dimensions of the array are compile time constants it is possible to make them template parameters also and avoid dynamic memory completely:
template <typename TType, int TRows, int TCols>
class TwoDArray {
private:
TType theArray[TRows][TCols];
TType defSpace;
....
TwoDArray<int, 5, 5> i(0);
see http://ideone.com/dEfZn5 for full demo.
Related
I have a Matrix template class and I need a function to set it's elements with variable number of args.
I should be able to call it like this:
aghMatrix<string> matrix;
matrix.setItems(2, 3, "torzmiae", "jestdnaci", "tablickore", "wyrazobed", "oelmntai", "rozmiaecy");
Where first integer is rows number, second is columns and rest (R * C) arguments are elements that I should put into matrix.
It should work with any data types, not only primitive ones.
For now, my function looks like this:
template<typename T>
template<typename... ARGS>
void aghMatrix<T>::setItems(const int rows, const int cols, ARGS... args) {
array<T, sizeof...(args)>unpacked_args {args...};
int row = 0;
int col = 0;
for (T arg : unpacked_args)
{
this->matrixPtr[row][col] = arg;
col++;
if (col == this->cols) {
row++;
col = 0;
}
}
return;
}
I assumed my matrix object is able to hold all elements. It does compile with many warnings about casting everything to unsigned int, but the program doesn't work anyway (it freezes on start).
Class declaration:
template<typename T>
class aghMatrix {
public:
[...]
template<typename... ARGS> void setItems(const int rows, const int cols, ARGS... args);
[...]
private:
T **matrixPtr;
int rows;
int cols;
void createMatrix(const int row, const int col);
bool checkRowCol(const int row, const int col) const;
};
Github project
EDIT: Oops! I just noticed you said "non recursive," so I presume the following pattern doesn't work for you. I'll still leave it hanging here for now, but I have provided also a non recursive solution below (which is based on va_list and hence only works with POD types)
If I understand correctly what you want to do, then you probably want the recursive variadic argument unpacking pattern; something like this seems to do the trick...
#include <iostream>
using namespace std;
// Helper for build_matrix, taking zero variadic arguments.
// This serves as the termination in the recursive unpacking of the args.
template<typename T>
void build_matrix_helper(T**, size_t, size_t, size_t, size_t) { return; }
// Helper for build_matrix, taking at least one variadic argument.
template <typename T, typename ...ARGS>
void build_matrix_helper(T** matrix, size_t curr_row, size_t curr_col,
size_t row, size_t col, const T& first, ARGS...rest) {
if (curr_col < col) {
matrix[curr_row][curr_col] = first;
++curr_col;
return build_matrix_helper<T>(matrix, curr_row, curr_col, row, col, rest...);
}
else {
++curr_row;
curr_col = 0;
return build_matrix_helper<T>(matrix, curr_row, curr_col, row, col, first, rest...);
}
return;
}
// Bare bones implementation.
template<typename T, typename ...ARGS>
T **build_matrix(size_t row, size_t col, ARGS...elements) {
T **new_mat = new T*[row];
for (size_t j = 0; j < row; ++j)
new_mat[j] = new T[col];
build_matrix_helper<T>(new_mat, 0, 0, row, col, elements...);
return new_mat;
}
int main() {
int **nm = build_matrix<int>(2, 3, 1, 2, 3, 4, 5, 6);
for (size_t i = 0; i < 2; ++i) {
cout << "[" << i + 1 << "]: ";
for (size_t j = 0; j < 3; ++j)
cout << nm[i][j] << " ";
cout << endl;
}
delete[] nm;
return 0;
}
In general, you want to avoid any direct manipulation of memory as much as possible. Also avoid as much as possible any casting voodoo unless you absolutely need it (which also ties in with direct memory manipulation).
Anyway, can use a non recursive solution below, using std::va_list.
NOTE Since this uses va_list, it does not work with non POD types.
#include <iostream>
#include <cstdarg>
using namespace std;
template <typename T>
T **build_matrix(size_t row, size_t col, ...) {
va_list args;
T **matrix = new T*[row];
va_start(args, col);
for (size_t i = 0; i < row; ++i) {
matrix[i] = new T[col];
for (size_t j = 0; j < col; ++j)
matrix[i][j] = va_arg(args, T);
}
va_end(args);
return matrix;
}
int main() {
int **nm = build_matrix<int>(2, 3, 1, 2, 3, 4, 5, 6);
for (size_t i = 0; i < 2; ++i) {
cout << "[" << i + 1 << "]: ";
for (size_t j = 0; j < 3; ++j)
cout << nm[i][j] << " ";
cout << endl;
}
delete[] nm;
return 0;
}
EDIT Initializer lists
As has been suggested in the comments to your OP, it is better to use initializer lists. I know this isn't what you asked for originally, but maybe it's worth considering:
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
T **build_matrix(size_t row, size_t col, initializer_list<T> il) {
if (il.size() != row*col)
throw out_of_range("Number of elements does not match matrix dimensions!");
size_t curr_row = 0;
size_t curr_col = 0;
T **nm = new T*[row];
nm[0] = new T[col];
for (T elm : il) {
if (curr_col == col) {
++curr_row;
nm[curr_row] = new T[col];
curr_col = 0;
}
nm[curr_row][curr_col] = elm;
++curr_col;
}
return nm;
}
int main() {
int **nm = build_matrix<int>(2, 3, {1, 2, 3, 4, 5, 6});
for (size_t i = 0; i < 2; ++i) {
cout << "[" << i + 1 << "]: ";
for (size_t j = 0; j < 3; ++j)
cout << nm[i][j] << " ";
cout << endl;
}
delete[] nm;
return 0;
}
There are two files:
Matrix.hpp:
template <typename T>
class Matrix {
private:
size_t rows = 0;
size_t cols = 0;
T* data = nullptr;
public:
Matrix() = default;
~Matrix();
Matrix(size_t n, size_t m);
T& operator() (size_t i, size_t j);
};
template <typename T>
Matrix<T>::Matrix(size_t n, size_t m) : rows(n), cols(m) {
try {
data = new T[rows*cols];
} catch (const std::bad_alloc& e) {
std::exit(EXIT_FAILURE);
}
};
template <typename T>
Matrix<T>::~Matrix() {
delete[] data;
}
template <typename T>
T& Matrix<T>::operator()(size_t i, size_t j) {
if (i < rows && i >= 0 && j < cols && j >= 0) {
return data[(i+1)*cols + (j+1)];
} else {
throw std::logic_error("matrix indices out of range");
}
}
and
Main.cpp:
#include <iostream>
#include "Matrix.hpp"
int main() {
size_t n, k;
std::cin >> n >> k;
Matrix<long double> m = {n, k};
for (size_t i = 0; i < m.getNumRows(); ++i) {
for (size_t j = 0; j < m.getNumCols(); ++j) {
std::cin >> m(i,j);
}
}
for (size_t i = 0; i < m.getNumRows(); ++i) {
std::cout << "\n";
for (size_t j = 0; j < m.getNumCols(); ++j) {
std::cout << m(i, j) << " ";
}
}
return 0;
}
When I'm entering something like:
1 2 3 4
I can get
3 4
as answer but sometimes the same input causes Segmentation fault 11 moreover when I'm changing template argument from long double to int, error disappears. How can I fix it?
Your operator function is accessing data outside its bounds.
Passing (0,0) to the function should return data[0]. Currently it returns data[2].
Change line
return data[(i+1)*cols+(j+1)];
to
return data[i*cols+j];
This is an extension to a previous question I previously asked. I can use a template to submit a dynamic 2D array and access the elements. Now, let's say I have a pointer to such an array. I am currently using the method shown here to assign my pointer to the original 2d array. I initially thought I could just change the expected input of the template function to (*A)[M][N] but that's a no-go. Am I missing some concept regarding pointers that someone could kindly explain?
header.h
#pragma once
#include <cstddef>
#include <iostream>
using namespace std;
template<size_t N, size_t M>
void printPtr( int(*A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
}
main.cpp
#include "header.h"
#include <iostream>
using namespace std;
int main() {
int A[][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
};
int (*ptrA)[3] = A;
printPtr(ptrA);
}
If you're not interested in knowing why your code isn't working in the first place just skip to the end of this post
The problem with your code is that you're "simulating" an array decaying by passing your pointer along:
int A[][2] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
};
int (*ptrA)[3] = A;
This is confirmed by the following code which uses C++11 features:
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T, typename U>
struct decay_equiv :
std::is_same<typename std::decay<T>::type, U>::type
{};
int main() {
int A[][4] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
std::cout << std::boolalpha
<< decay_equiv<decltype(A), int(*)[3]>::value << '\n'; // Prints "true"
}
Example
You should either pass a non-decayed type (i.e. a type that has all the information regarding the array dimension) to the function via a pointer or a reference:
#include <cstddef>
#include <iostream>
using namespace std;
template<size_t N, size_t M>
void printPtr(int (*A)[M][N] /* Also a reference could work */) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << (*A)[i][j] << " ";
}
cout << endl;
}
}
int main() {
int A[][6] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
int(*ptrA)[4][7] = &A; // Not a decayed type
printPtr(ptrA);
}
Example
Another solution would be to not use a pointer to the array in the first place or dereference it when passing a reference to the array:
template<size_t N, size_t M>
void printPtr( int(&A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
}
...
printPtr(A);
printPtr(*ptrA);
Example
You defined the function parameter as a pointer to a two-dimensional array
int(*A)[M][N])
while are trying to call the function passing as the argument a pointer to a one-dimensional array
int (*ptrA)[3] = A;
printPtr(ptrA);
Moreover the function itself is invalid. It shall look like
template<size_t N, size_t M>
void printPtr( int(*A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << ( *A )[i][j] << " ";
}
cout << endl;
}
}
that is you have to use expression ( *A )[i][j] instead of A[i][j]
So you need to change the function the way shown above and to use appropriate pointer as the argument
int (*ptrA)[4][3] = &A;
printPtr(ptrA);
Of course it would be better to define the function parameter as reference to a two-dimensional array. For example
template<size_t N, size_t M>
void printPtr( int(&A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
}
In this case you could call the function like
printPtr( A );
It should be
template<size_t N, size_t M>
void printPtr(const int(&A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
}
calling with:
printPtr(A);
or
template<size_t N, size_t M>
void printPtr(const int(*A)[M][N]) {
for(int i=0; i < M; i++){
for(int j=0; j<N; j++) {
cout << (*A)[i][j] << " ";
}
cout << endl;
}
}
calling with:
printPtr(&A);
BTW your pointer should be:
int (*ptrA)[4][3] = &A;
For a class assignment I am required to create 4 functions to test in a program. I must use the copyArray function, PrintArray function and InputArray function. The problem that I am having the most trouble with is the copyArray portion. I have completed most of the code on my own but I need to know if I am close to the solution or not close at all.The code, I'm sure, is a mess. If someone could help me get in the right direction of how to finish this I will be very grateful.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
void inputArray(istream &, int[], int*);
void printArray(ostream &, const int[], int);
float a[4] = { 0, 1, 2, 3 };
float b[4];
void copyArray(const int orig[], int dup[], int);
void main()
{
int a[4] = { 7, 14, 9, 10 };
int b[4];
cout << "The input data : " << endl;
inputArray(cin, a, b);
cout << "The printArray data : " << endl;
printArray(cout, b, 4);
}
//----------------------------------------------------------------------------
void inputArray(istream & in, int t[], int howMany)
{
for (int i = 0; i < howMany; i++)
in >> t[i];
return;
}
//-----------------------------------------------------------------------------
void printArray(ostream & out, const int r[], int cnt)
{
for (int i = 0; i< cnt; i++)
out << r[i] << endl;
return;
}
void copyArray(const int orig [], int dup [], int);
for (int i = 0; i < 4; i++){
b[i] = a[i];
}
}
Of course it would be better to define function copyArray as a template function. For example
template <typename T, size_t N>
void copyArray( T ( &dst )[N], const T ( &src )[N] )
{
for ( size_t i = 0; i < N; i++ ) dst[i] = src[i];
}
As for your function declaration then its definition can look like
void copyArray( int dst[], const int src[], size_t n )
for ( size_t i = 0; i < n; i++ ) dst[i] = src[i];
}
In the function definition shown by you you have to remove the semicolon after the closing parenthesis and to use function parameters instead of the global variables.
Take into account that there are standard algorithms std::copy, std::copy_n, std::copy_if, std::copy_backward declared in header <algorithm> that can be used for coping arrays.
I am trying to take in an input for the dimensions of a 2D matrix. And then use user input to fill in this matrix. The way I tried doing this is via vectors (vectors of vectors). But I have encountered some errors whenever I try to read in data and append it to the matrix.
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
for(int j = 0; j<CC; j++)
{
cout<<"Enter the number for Matrix 1";
cin>>matrix[i][j];
}
}
Whenever I try to do this, it gives me a subscript out of range error. Any advice?
You have to initialize the vector of vectors to the appropriate size before accessing any elements. You can do it like this:
// assumes using std::vector for brevity
vector<vector<int>> matrix(RR, vector<int>(CC));
This creates a vector of RR size CC vectors, filled with 0.
As it is, both dimensions of your vector are 0.
Instead, initialize the vector as this:
vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )
matrix[i].resize(CC);
This will give you a matrix of dimensions RR * CC with all elements set to 0.
I'm not familiar with c++, but a quick look at the documentation suggests that this should work:
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
vector<int> myvector;
for(int j = 0; j<CC; j++)
{
int tempVal = 0;
cout<<"Enter the number for Matrix 1";
cin>>tempVal;
myvector.push_back(tempVal);
}
matrix.push_back(myvector);
}
Assume we have the following class:
#include <vector>
class Matrix {
private:
std::vector<std::vector<int>> data;
};
First of all I would like suggest you to implement a default constructor:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
private:
std::vector<std::vector<int>> data;
};
At this time we can create Matrix instance as follows:
Matrix one;
The next strategic step is to implement a Reset method, which takes two integer parameters that specify the new number of rows and columns of the matrix, respectively:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
if (rows == 0 || cols == 0) {
data.assign(0, std::vector<int>(0));
} else {
data.assign(rows, std::vector<int>(cols));
}
}
private:
std::vector<std::vector<int>> data;
};
At this time the Reset method changes the dimensions of the 2D-matrix to the given ones and resets all its elements. Let me show you a bit later why we may need this.
Well, we can create and initialize our matrix:
Matrix two(3, 5);
Lets add info methods for our matrix:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
At this time we can get some trivial matrix debug info:
#include <iostream>
void MatrixInfo(const Matrix& m) {
std::cout << "{ \"rows\": " << m.GetNumRows()
<< ", \"cols\": " << m.GetNumColumns() << " }" << std::endl;
}
int main() {
Matrix three(3, 4);
MatrixInfo(three);
}
The second class method we need at this time is At. A sort of getter for our private data:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int At(const int &row, const int &col) const {
return data.at(row).at(col);
}
int& At(const int &row, const int &col) {
return data.at(row).at(col);
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
The constant At method takes the row number and column number and returns the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
std::cout << three.At(1, 2); // 0 at this time
}
The second, non-constant At method with the same parameters returns a reference to the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
three.At(1, 2) = 8;
std::cout << three.At(1, 2); // 8
}
Finally lets implement >> operator:
#include <iostream>
std::istream& operator>>(std::istream& stream, Matrix &matrix) {
int row = 0, col = 0;
stream >> row >> col;
matrix.Reset(row, col);
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
stream >> matrix.At(r, c);
}
}
return stream;
}
And test it:
#include <iostream>
int main() {
Matrix four; // An empty matrix
MatrixInfo(four);
// Example output:
//
// { "rows": 0, "cols": 0 }
std::cin >> four;
// Example input
//
// 2 3
// 4 -1 10
// 8 7 13
MatrixInfo(four);
// Example output:
//
// { "rows": 2, "cols": 3 }
}
Feel free to add out of range check. I hope this example helps you :)
try this. m = row, n = col
vector<vector<int>> matrix(m, vector<int>(n));
for(i = 0;i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
cout << endl;
}
cout << "::matrix::" << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
Vector needs to be initialized before using it as cin>>v[i][j]. Even if it was 1D vector, it still needs an initialization, see this link
After initialization there will be no errors, see this link
What you have initialized is a vector of vectors, so you definitely have to include a vector to be inserted("Pushed" in the terminology of vectors) in the original vector you have named matrix in your example.
One more thing, you cannot directly insert values in the vector using the operator "cin". Use a variable which takes input and then insert the same in the vector.
Please try this out :
int num;
for(int i=0; i<RR; i++){
vector<int>inter_mat; //Intermediate matrix to help insert(push) contents of whole row at a time
for(int j=0; j<CC; j++){
cin>>num; //Extra variable in helping push our number to vector
vin.push_back(num); //Inserting numbers in a row, one by one
}
v.push_back(vin); //Inserting the whole row at once to original 2D matrix
}
I did this class for that purpose. it produces a variable size matrix ( expandable) when more items are added
'''
#pragma once
#include<vector>
#include<iostream>
#include<iomanip>
using namespace std;
template <class T>class Matrix
{
public:
Matrix() = default;
bool AddItem(unsigned r, unsigned c, T value)
{
if (r >= Rows_count)
{
Rows.resize(r + 1);
Rows_count = r + 1;
}
else
{
Rows.resize(Rows_count);
}
if (c >= Columns_Count )
{
for (std::vector<T>& row : Rows)
{
row.resize(c + 1);
}
Columns_Count = c + 1;
}
else
{
for (std::vector<T>& row : Rows)
{
row.resize(Columns_Count);
}
}
if (r < Rows.size())
if (c < static_cast<std::vector<T>>(Rows.at(r)).size())
{
(Rows.at(r)).at(c) = value;
}
else
{
cout << Rows.at(r).size() << " greater than " << c << endl;
}
else
cout << "ERROR" << endl;
return true;
}
void Show()
{
std::cout << "*****************"<<std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " <<setw(5)<< c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
void Show(size_t n)
{
std::cout << "*****************" << std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " << setw(n) << c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
// ~Matrix();
public:
std::vector<std::vector<T>> Rows;
unsigned Rows_count;
unsigned Columns_Count;
};
'''