在不同的数据类型之间交替 [英] Alternate between different data-types

查看:157
本文介绍了在不同的数据类型之间交替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄明白这一点,这真的让我很恼火。我有一个函数,将数组或向量转换为一个复数的向量,但是,我不知道如何可能的功能,能够接受双阵列,以及双向量。我试过使用模板,但是,这似乎不work.template

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) {


};

我希望的是:

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.

推荐答案

您可以使您的函数和构造函数具有两个迭代器的模板:

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);
  }
  .... 
};

那么

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());

我省略了 foo 对我来说很清楚它的作用是什么。

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

这篇关于在不同的数据类型之间交替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆