This is my attempt at passing a N-D vector of arbitrary element types, inner and outer lengths to a class and printing its contents. I know there are a few issues but I have not been able to resolve. How would I assign the arbitrary inner vector length to a variable or set of variables? I have attempted to pass iterators to the constructor and assign them to member variables (with and without passing the vector) but that has not been successful. I understand there are libraries for handling matrix objects and other questions which address problems similar to the one posed herein, however, there are unique/fringe scenarios which comprise the motivation for this question.
#include <iostream>
#include <iterator>
#include <vector>
template <typename V, typename I>
class myVec{
private:
I rows;
I cols;
std::vector< std::vector<V> > vec( rows , vector<int> (cols));
public:
myVec(std::vector< std::vector<V> > const &myArr){
vec = myArr;
rows = myArr.size();
cols = myArr[].size();
}
void printVec(){
for(int i = 0; i < vec.size(); i++){
std::cout << "{";
for(int j = 0; j < vec[i].size(); j++){
std::cout << vec[i][j];
}
if (i < vec.size()){
std::cout << "}";
if (i < vec.size()-1){
std::cout << ",";
std::cout << " ";
}
}
}
std::cout << std::endl;
}
};
int main () {
std::vector< std::vector<int> > mainVec = {{1, 2, 4}, {5, 6, 7, 8}, {4, 3, 2, 1, 7}, {8, 7, 6, 5, 9, 3}};
myVec<int, int> vec1 = myVec<int, int>(mainVec);
vec1.printVec();
return 0;