Ícaro Lorran
Ícaro Lorran

Reputation: 96

Guess type of matrix in template

I am practicing c++ templates by trying to write a code where I can save a matrix of an arbitrary type to a csv file. I already have some coding working except that I have to pass an useless parameter so that the function knows what type of data composes the matrix. How do I get rid of it?

#include<vector>
#include<fstream>
#include<iostream>

using namespace std;

template <
typename num,
class Matrix = vector<vector<num>>
>

void matrix_to_csv(Matrix X, string filename, num useless){
  fstream file;
  file.open(filename, fstream::out);
  for(int i = 0; i<X.size(); i++){
    for(int j = 0; j<X[0].size(); j++){
      file << X[i][j] <<",";
    }
    file<<"\n";
  }
  file.close();
}

Example:

vector<vector<int>> M = {
                        {1,2,5,8},
                        {4,3,1,2},
                        {1,6,5,4}
                        };
matrix_to_csv(M,"result.csv",0);

Upvotes: 0

Views: 49

Answers (2)

user10957435
user10957435

Reputation:

There is no way to completely "get rid of it" in the sense that if the compiler can't deduce the type from x, then you will need some way for the compiler to know the type.

That said, instead of passing a parameter you don't use, you do have this instead:

matrix_to_csv<num>(X,"result.csv");

Where num is the type that you want.


Note: If you are not using Matrix anywhere else, you do have the option to make your template header simpler:

template <typename num>
void matrix_to_csv(vector<vector<num>> X, string filename){

However, this is just personal preference, I suppose.

Upvotes: 0

Thomas Sablik
Thomas Sablik

Reputation: 16454

You can remove Matrix and use the template parameter like that

#include<vector>
#include<fstream>
#include<iostream>

using namespace std;

template <typename num>
void matrix_to_csv(vector<vector<num>> X, string filename){
  fstream file;
  file.open(filename, fstream::out);
  for(const auto &row : X){
    for(const auto &el : row) {
      file << el <<",";
    }
    file<<"\n";
  }
  file.close();
}

int main() {
    vector<vector<int>> M = {
                            {1,2,5,8},
                            {4,3,1,2},
                            {1,6,5,4}
                            };
    matrix_to_csv(M,"result.csv");
}

In your example vector<vector<num>> is the default parameter if no parameter is given. But you pass vector<vector<int>> and Matrix is set to vector<vector<int>>. There is no relation between int and num in your example. You can alternatively use Matrix instead of num

#include<vector>
#include<fstream>
#include<iostream>

using namespace std;

template <typename Matrix>
void matrix_to_csv(Matrix X, string filename){
  fstream file;
  file.open(filename, fstream::out);
  for(const auto &row : X){
    for(const auto &el : row) {
      file << el <<",";
    }
    file<<"\n";
  }
  file.close();
}

int main() {
    vector<vector<int>> M = {
                            {1,2,5,8},
                            {4,3,1,2},
                            {1,6,5,4}
                            };
    matrix_to_csv(M,"result.csv");
}

Upvotes: 1

Related Questions