Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate between different data-types

I'm trying to figure this out, and, it's really annoying me. I have a function that converts either an array or a vector into a vector of complex numbers, but, I do not know how it would be possible for the function to be able to accept both double arrays, as well as double vectors. I've tried using templates, but, this does not seem to work.template

template<typename T>
vector<Complex::complex> convertToComplex(T &vals)
{

}


Value::Value(vector<double> &vals, int N) {

};


Value::Value(double *vals, int N) {


};

What I am hoping for is this:

int main()
{
   double[] vals = {1, 2, 3, 4, 5};
   int foo = 4;
   Value v(vals, foo); // this would work and pass the array to the constructor, which would
                  // then pass the values to the function and covert this to a 
                  //   vector<complex>
}

I could do the same for a vector as well.. I don't know whether or not templates are the right approach for this.

like image 235
Phorce Avatar asked Dec 02 '25 23:12

Phorce


1 Answers

You could make your function and constructor a template that takes two iterators:

template<typename Iterator>
vector<Complex::complex> convertToComplex(Iterator begin, Iterator end)
{

}

class Value
{
 public:
  template <Iteraror>
  Value(Iterator begin, Iterator end)
  {
    vector<Complex::complex> vec = comvertToComplex(begin, end);
  }
  .... 
};

then

double[] vals = {1, 2, 3, 4, 5};
Value v(std::begin(vals), std::end(vals)); 

std::vector<double> vec{1,2,3,4,5,6,7};
Value v2(v.begin(), v.end());

I have omitted foo because it isn't very clear to me what its role is.

like image 147
juanchopanza Avatar answered Dec 05 '25 13:12

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!