I'm trying to overload [ ] for 2d arrays so that, for example, a[1] converts the first row of the 2d array into a vector and returns it. Then a seperate function that takes it and prints out the entire vector.
matrix<int> a; //pointer ** points to array of pointers which point to their respective array
...
PrintVec(a[0]) //print first array as a vector
Here's what I have so far, I'm not sure how to really approach this. But this is the general idea
//overload [] to create vector from single array from 2d array
matrix<T> operator[] (int i){
vector<T> temp(array_[i][], num_cols_); //create vector of size column then
//copy ith array (pointer pointing to ith row) from 2d array into vector
return temp;
}
template <typename T> //I'm sure this is wrong
void PrintVec(vector<T> & v){
for (auto i = v.begin(); i != v.end(); i++){
cout << v[i] << " " << endl;
}
}
Edit: Here's the implementation of matrix
private:
size_t num_cols_;
size_t num_rows_;
Comparable **array_;
public:
void ReadMatrix();
template <typename T>
void matrix <T>::ReadMatrix(){
cout << "Enter rows:" << endl;
cin >> num_rows_;
cout << "Enter columns:" << endl;
cin >> num_cols_;
array_ = new T*[num_rows_];
for (int i = 0; i < num_rows_; i++){
array_[i] = new T[num_cols_];
}
cout << "Enter values for matrix:" << endl;
for (int j = 0; j < num_rows_; j++){
for (int k = 0; k < num_cols_; k++){
cin >> array_[j][k];
}
}
}