I have a c++ class of matrices with only one field:
template <class T>
class matrix {
private:
std::vector < std::vector <T>> elements;
public:
...
}
Also I have overdriven operator []:
std::vector <T>& operator[] (int index) {
return elements[index];
}
const std::vector <T>& operator[] (int index) const {
return elements[index];
}
And I can use it in code
matrix <double> A(5,5);
A[1][3] = 5;
But I can't add such expressions to debug window. I add A[2] or A[2][3] and get no operator "[]" corresponding to these operands. How can I fix this?
Related
hi i am trying to create matrix class and i want to assign like matrix[0][2]=3
i checked form and found a solution with array but i want to do with vector and could not understand why does not it works ?
template<class T>
class Matrix
{
public:
Matrix(int a, int b)
{
vector<vector<T> > vec( a , vector<T> (b, 0));
matrixData = vec;
}
class Array1D
{
public:
Array1D(vector<T> a):temp(a) {}
T& operator[](int a)
{
return temp[a];
}
vector<T> temp;
};
vector<vector<T> > matrixData;
Array1D operator[] (int a)
{
return Array1D(matrixData[a]);
}
};
int main()
{
Matrix<int> n(3,5);
n[0][2] = 123; //assign
cout<<n[0][2]; // wrong output getting 0
}
You have to change Array1D implementation to:
class Array1D
{
public:
Array1D(vector<T>& a):temp(a) {}
T& operator[](int a)
{
return temp[a];
}
vector<T>& temp;
};
without this, every time you call operator[] on matrix you access temporary vector. Hence, every call n[0] works on different vector. So any previous modifications cannot be saved, and you always see 0 as result.
With the above change, you access original vector of matrix by Array1D proxy class.
Demo
You are returning the wrong type from the Matrix::operator[]. You need to return a nested vector by reference, so that you can chain the next [] to it:
vector<T>& operator[] (int a)
{
return matrixData[a];
}
In fact, you don't need the internal Array1D class at all for this purpose, since vector already has an operator[], and you can remove it entirely.
Here's a working demo.
This question already has answers here:
Operator[][] overload
(17 answers)
Closed 8 years ago.
I need a two dimensional array where the length of neither dimension is known at compile time. I want [][] access.
There are several questions about this already, suggesting boost::multi_array, std::vector<std::vector<type>>, allocating one array for the x dimension and X arrays for the y dimension, so on and so forth.
The catch is that I do not control the data, it already exists as a single contiguous array (size x*y). I have a pointer to it and the size of both dimensions, and I am more or less wrapping it to get [][] access.
I would like to avoid creating a whole bunch of objects (like allocating an array of std::vectors and pointing them all at the right things), and boost.
I considered creating a class to hold both dimensions and the pointer, and overloading [][], but that doesn't work because [][] is two operators, and the second [] applies to a different object.
Ultimately I'm looking for something that amounts to using [][] as syntactic sugar for some kind of access(int x, int y) function. Is that possible?
You may wrap it in a class and overload operator [], something like:
template <typename T>
class MultiArray
{
public:
explicit MultiArray(T* arr, int sizex, int sizey) : data(arr), sizey(sizey) {}
const T* operator [] (int x) const { return &data[x * sizey]; }
T* operator [] (int x) { return &data[x * sizey]; }
private:
T* data;
int sizey;
};
Live example
You need to have two classes DataTypeP and DataTypePP. While initializing DataTypePP, you will need to distribute the memory chunks to different DataTypeP references.
class DataTypeP {
int *ptr;
public:
int operator[](int y){ return ptr[y];}
};
class DataTypePP{
DataTypeP *bPtr;
public:
DataTypeP operator[](int x){ return bPtr[x];}
};
int main(){
DataTypePP a;
cout<<a[1][2];
return 0;
}
With a std::vector<std::vector<type*>>, you can build the inside vector using custom input operator that iterate over your data and return a pointer to each data.
For example:
size_t w, h;
int* myData = retrieveData(&w, &h);
std::vector<std::vector<int*>> data;
data.reserve(w);
template<typename T>
struct myIterator : public std::iterator<std::input_iterator_tag, T*>
{
myIterator(T* data) :
_data(data)
{}
T* _data;
bool operator==(const myIterator& rhs){return rhs.data == data;}
bool operator!=(const myIterator& rhs){return rhs.data != data;}
T* operator*(){return data;}
T* operator->(){return data;}
myIterator& operator++(){data = &data[1]; return *this; }
};
for (size_t i = 0; i < w; ++i)
{
data.emplace_back(myIterator<int>(&myData[i * h]),
myIterator<int>(&myData[(i + 1) * h]));
}
Live example
This solution has the advantage of providing you with a real STL container, so you can use special for loops, STL algorithms, and so on.
for (const auto& i : data)
for (const auto& j : i)
std::cout << *j << std::endl;
std::cout << "or with index access: " << std::endl;
for (size_t i = 0; i < w; ++i)
for (size_t j = 0; j < h; ++j)
std::cout << *data[i][j] << std::endl;
However, it does create vectors of pointers, so if you're using small datastructures such as this one you can directly copy the content inside the array.
First I have Matrix template Matrix< typename T, int Roz> and specialization of this template Matrix. But the specialization is create with constructor where argument is a size of matrix. So in example code A and Z have the same dimension. So I want to make add operator work with it. But complier say: error: no match for 'operator+' in 'Z + A'. So how I need to write operator+ for Matrix wich will be work with Matrix?
Code of Matrix templates below:
template<typename T,int Roz>
class Matrix
{
public:
T tab[Roz][Roz];
int z=Roz;
Matrix()
{
for(int i=0;i<Roz;++i)
for(int j=0;j<Roz;++j)
tab[i][j]=0;
}
T& operator()(int x,int y){
return tab[x-1][y-1];
}
//Problematic operator
Matrix& operator+(Matrix<T,Roz> b)
{
Matrix<T,Roz> tmp;
for(int i=0;i<Roz;++i)
for(int j=0;j<Roz;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
friend ostream& operator<<(ostream& out, Matrix<T,Roz> &v)
{
for(int i=0;i<v.z;++i)
{
for(int j=0;j<v.z;++j)
out<<v.tab[i][j]<<" ";
out<<endl;
}
return out;
}
};
//Specialization template
template<class T>
class Matrix<T,0>
{
private:
Matrix()
{
}
public:
vector<vector<T> > tab;
int z=0;
Matrix(int z)
{
for(int i=0;i<z;++i)
tab.push_back(vector<T>(z));
this->z = z;
for(int i=0;i<z;++i)
for(int j=0;j<z;++j)
tab[i][j]=0;
}
T& operator()(int x,int y){
return tab[x-1][y-1];
}
//Problematic operator
Matrix& operator+(Matrix<T,0> b)
{
Matrix<T,0> tmp(b.z);
for(int i=0;i<z;++i)
for(int j=0;j<z;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
//Problematic operator
Matrix& operator+(Matrix<T,0> &b)
{
Matrix<T,0> tmp(b.z);
for(int i=0;i<z;++i)
for(int j=0;j<z;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
friend ostream& operator<<(ostream& out, Matrix<T,0> &v)
{
for(int i=0;i<v.z;++i)
{
for(int j=0;j<v.z;++j)
out<<v.tab[i][j]<<" ";
out<<endl;
}
return out;
}
};
When I try add for example Matrix < int,3 > and Matrix < int, 0 > this make error. How I must definie operator+ in both templates they will work together?May I have to overload operator+ in specialization template?
The code below makes error when I try add A + Z.
int main()
{
Matrix<int,3> A, B;
Matrix<int, 4> C;
Matrix<int, 0> Z(3);
A(1,1)=1;
B(1,1)=2;
Z(1,1)=1;
Z + A;//<------- here error between diffrent instance of Matrix template
}
I don't really know why you wish to add up matrices of different dimensions, but in order to do it, you must turn the method into a method template on the dimension of the matrix argument:
#include <type_traits>
template <int Roz2>
Matrix& operator+(Matrix<T,Roz2> b)
{
// check matrix argument size compatibility
std::static_assert(Roz2 >= Roz, "incompatible matrices for operator+()");
Matrix<T,Roz> tmp;
for(int i=0;i<Roz;++i)
for(int j=0;j<Roz;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
Since Matrix<T,0> is a specialization to handle dynamic dimension, you must specialize the operator for it:
// in the generic template
Matrix& operator+(Matrix<T,0> b)
{
assert (b.z < Roz, "incompatible dynamic matrix for operator+");
Matrix<T,0> tmp(b.Roz);;
for(int i=0;i<Roz;++i)
for(int j=0;j<Roz;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
and in Matrix<T,0>:
template <int Roz>
Matrix& operator+(Matrix<T,Roz> b)
{
assert(Roz >= z,"....");
Matrix<T,0> tmp(b.z);
for(int i=0;i<z;++i)
for(int j=0;j<z;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
In the above solution, however, the operator is not commutative. In order to make it so, we need to do some change:
// define a min operator as constexpr in case the existing one is not
template<typename T> constexpr
T const& rmin(T const& a, T const& b) {
return a > b ? b : a;
}
// generic operator
template <int Roz2>
Matrix<T,rmin(Roz,Roz2)>& operator+(Matrix<T,Roz2> b)
{
constexpr int R = min(Roz,Roz2);
Matrix<T,R> tmp;
for(int i=0;i<R;;++i)
for(int j=0;j<R;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
// specialized operator on Roz=0 argument
Matrix<T,0>& operator+(Matrix<T,0> b)
{
const int r = min(z,b.z);
Matrix<T,0> tmp(r);
for(int i=0;i<r;;++i)
for(int j=0;j<r;++j)
tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
// specialized operator in Roz=0 template
Matrix& operator+(Matrix<T,Roz> b)
{
return b + *this;
}
You will have to duplicate the code for const & parameters taking operators (or perhaps simply get rid of the non const parameter versions which don't seem very useful in that context).
Note the use of a static assert to limit the use of the operator with matrices of equal or larger dimension (this is a C++11 feature) in the non commutative version of the operators.
See this question on the topic of min not being a constexpr function template for further details.
just wrote a code for a template array class (I know its not finished yet), and trying to remember how to overload operators (meanwhile without special difficulties...).
Anyway, while thinking of how to implement operator[] I wondered what would happen if the index is outside the boundaries of the array... I'm pretty sure it is not possible for me to return a NULL (because of the return type), right? and if so, what should I return in case the index is out of boundries?
here's the code, most of it is redundant to my question, but it might help anyone who google's operators overloading so I post the complete code...
#ifndef __MYARRAY_H
#define __MYARRAY_H
#include <iostream>
using namespace std;
template <class T>
class MyArray
{
int phisicalSize, logicalSize;
char printDelimiter;
T* arr;
public:
MyArray(int size=10, char printDelimiter=' ');
MyArray(const MyArray& other);
~MyArray() {delete []arr;}
const MyArray& operator=(const MyArray& other);
const MyArray& operator+=(const T& newVal);
T& operator[](int index);
friend ostream& operator<<(ostream& os, const MyArray& ma)
{
for(int i=0; i<ma.logicalSize; i++)
os << ma.arr[i] << ma.printDelimiter;
return os;
}
};
template <class T>
T& MyArray<T>::operator[]( int index )
{
if (index < 0 || index > logicalSize)
{
//do what???
}
return arr[index];
}
template <class T>
const MyArray<T>& MyArray<T>::operator+=( const T& newVal )
{
if (logicalSize < phisicalSize)
{
arr[logicalSize] = newVal;
logicalSize++;
}
return *this;
}
template <class T>
const MyArray<T>& MyArray<T>::operator=( const MyArray<T>& other )
{
if (this != &other)
{
delete []arr;
phisicalSize = other.phisicalSize;
logicalSize = other.logicalSize;
printDelimiter = other.printDelimiter;
arr = new T[phisicalSize];
for(int i=0; i<logicalSize; i++)
arr[i] = other.arr[i];
}
return *this;
}
template <class T>
MyArray<T>::MyArray( const MyArray& other ) : arr(NULL)
{
*this = other;
}
template <class T>
MyArray<T>::MyArray( int size, char printDelimiter ) : phisicalSize(size), logicalSize(0), printDelimiter(printDelimiter)
{
arr = new T[phisicalSize];
}
#endif
operator[] generally does no bounds checking. Most of the standard containers that can have a range utilize a separate function, at(), which is range checked and throws a std::out_of_range exception.
You likely want to implement a const T& operator[] overload, too.
I spent several hours, completely stuck when I realized that only the templated version of my code has a bug.
In the following code, when pushing_back elements in the myMap, the original vectors myVec1 and myVec2 are modified and contain garbage at the end of the execution. If I un-template everything, just replacing template<T> by double, then the code works fine as I would expect (the original arrays are untouched).
The funny thing is if I put a cout in the copy constructor, it does not get called if the code is templated. But it gets called if I replace the copy constructor with Vector<T2> by the original type Vector<T>, and then everything work fine.
Why wouldn't the compiler know that T2==T since I only use double?
(note, the code has been made as short as possible so as to show the errors - I thus removed accessors, made everything public etc.).
#include <vector>
#include <map>
template<class T>
class Vector{
public:
Vector():n(0),data(0){};
Vector(int N):n(N),data(new T[N]){};
Vector(T x, T y):n(2),data(new T[2]){data[0]=x; data[1]=y;};
template<class T2> Vector(const Vector<T2>& rhs):n(rhs.n), data(new T[n])
{
for (int i=0; i<n; i++)
data[i] = T(rhs.data[i]);
}
~Vector(){delete[] data;}
Vector& operator=(const Vector& rhs)
{
if (rhs.n != n)
{
if (data)
delete[] data;
data = new T[rhs.n];
}
n = rhs.n;
memcpy(data, rhs.data, n*sizeof(T));
return *this;
}
T& operator[](int i){return data[i];}
const T& operator[](int i) const {return data[i];}
int n;
T* data;
};
typedef Vector<double> Vectord;
template <class T> inline bool operator<(const Vector<T>& v1, const Vector<T>& v2)
{
for (int i=0; i<v1.n; i++)
{
if (v1[i]<v2[i]) return true;
if (v1[i]>v2[i]) return false;
}
return false;
}
int main(int argc, char** argv)
{
std::vector<Vectord> myVec1(3);
myVec1[0] = Vectord(1.,3.);
myVec1[1] = Vectord(3.,3.);
myVec1[2] = Vectord(1.,5.);
std::vector<Vectord> myVec2(3);
myVec2[0] = Vectord(4.,1.);
myVec2[1] = Vectord(2.,5.);
myVec2[2] = Vectord(6.,5.);
std::map<Vectord, std::vector<Vectord> > myMap;
for (int i=0; i<3; i++)
{
myMap[myVec1[i]].push_back(myVec2[i]);
}
return 0;
}
A templated constructor is never a copy constructor.
So your class is using the automatically generated copy constructor.
Cheers & hth.,