如何更改参数名称?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何更改参数名称?相关的知识,希望对你有一定的参考价值。

首先有一个 vector 大小为20。我们将从0到100的随机数分配至 vector. 然后,利用模板函数,我们找到这个向量元素的总和、平均值、中位数和标准差。代码如下,屏幕输出如下。

#include <numeric> 
#include <cmath>
#include <algorithm> 
#include <functional> 
#include <vector> 
#include <iostream> 
#include <ctime>        
#include <cstdlib>

using namespace std;

template<int N, class T>
T nthPower(T x) {
  T ret = x;
  for (int i=1; i < N; ++i) {
    ret *= x;
  }
  return ret;
}

template<class T, int N>
struct SumDiffNthPower {
  SumDiffNthPower(T x) : mean_(x) { };
  T operator( )(T sum, T current) {
    return sum + nthPower<N>(current - mean_);
  }
  T mean_;
};

template<class T, int N, class Iter_T>
T nthMoment(Iter_T first, Iter_T last, T mean)  {
  size_t cnt = distance(first, last);
  return accumulate(first, last, T( ), SumDiffNthPower<T, N>(mean)) / cnt;
}

template<class T, class Iter_T>
T computeVariance(Iter_T first, Iter_T last, T mean) {
  return nthMoment<T, 2>(first, last, mean);
}

template<class T, class Iter_T>
T computeStdDev(Iter_T first, Iter_T last, T mean) {
  return sqrt(computeVariance(first, last, mean));
}



template<class T, class Iter_T>
void computeStats(Iter_T first, Iter_T last, T& sum, T& mean,
          T& var, T& std_dev)
{
  size_t cnt = distance(first, last);
  sum = accumulate(first, last, T( ));
  mean = sum / cnt;
  var = computeVariance(first, last, mean);
  std_dev = sqrt(var);

}

int RandomNumber () { return (std::rand()%100); }

int main(int argc, char **argv) {


  std::srand ( unsigned ( std::time(0) ) );  

  vector<int> v(20);

  std::generate (v.begin(), v.end(), RandomNumber);  

  double sum, mean, var, dev;

  computeStats(v.begin( ), v.end( ), sum, mean, var, dev);

  cout << "count = " << v.size( ) << "
";
  cout << "sum = " << sum << "
";
  cout << "mean = " << mean << "
";
  cout << "variance = " << var << "
";
  cout << "standard deviation = " << dev << "
";
  cout << endl;



}

输出为:count = 20 sum = 789 mean = 39.45 variance = 888.448 standard deviation = 29.8068。

我怎么能改变参数呢,我不想用这种风格。

template<class T, class Iter_T>
T computeVariance(Iter_T first, Iter_T last, T mean) {
  return nthMoment<T, 2>(first, last, mean);
}

我想用这种方式来计算东西。

template<class T, class T2>
T computeVariance(vector<T2> &vec, T mean)
{
... 
}

所有的函数都应该是这样的。我该怎么做呢?

在主函数中,我如何使用生成函数来复制向量,如何将参数的地址发送到

std::generate (v.begin(), v.end(), RandomNumber);

computeStats(v.begin( ), v.end( ), sum, mean, var, dev);
答案
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

template <int N, class T>
T nthPower(T x) {
    T ret = x;
    for (int i = 1; i < N; ++i) {
        ret *= x;
    }
    return ret;
}

template <class T, int N>
struct SumDiffNthPower {
    SumDiffNthPower(T x) : mean_(x){};
    T operator()(T sum, T current) {
        return sum + nthPower<N>(current - mean_);
    }
    T mean_;
};

template <class T, int N, class T2>
T nthMoment(vector<T2> &vec, T mean) {
    size_t cnt = vec.size();
    return accumulate(vec.begin(), vec.end(), T(), SumDiffNthPower<T, N>(mean)) / cnt;
}

template <class T, class T2>
T computeVariance(vector<T2> &vec, T mean) {
    return nthMoment<T, 2>(vec, mean);
}

template <class T, class T2>
T computeStdDev(vector<T2> &vec, T mean) {
    return sqrt(computeVariance(vec, mean));
}

template <class T, class T2>
void computeStats(vector<T2> &vec, T &sum, T &mean, T &var, T &std_dev) {
    size_t cnt = vec.size();
    sum = accumulate(vec.begin(), vec.end(), T());
    mean = sum / cnt;
    var = computeVariance(vec, mean);
    std_dev = sqrt(var);
}

int RandomNumber() { return (std::rand() % 100); }

int main(int argc, char **argv) {
    std::srand(unsigned(std::time(0)));
    vector<int> v(20);
    std::generate(v.begin(), v.end(), RandomNumber);
    double sum, mean, var, dev;
    computeStats(v, sum, mean, var, dev);
    cout << "count = " << v.size() << "
";
    cout << "sum = " << sum << "
";
    cout << "mean = " << mean << "
";
    cout << "variance = " << var << "
";
    cout << "standard deviation = " << dev << "
";
    cout << endl;
    return 0;
}

我希望这能行得通。PS: 我不明白你的通过参数传递的事情。在评论中解释一下,也许我可以帮助你。

另一答案

你可以有

template<class T, class T2>
T computeVariance(vector<T2> &vec, T mean)
{
    return nthMoment<T, 2>(vec.begin(), vec.end(), mean);
}

以上是关于如何更改参数名称?的主要内容,如果未能解决你的问题,请参考以下文章

如何更改参数名称?

如何从活动中更改片段中视图的可见性

更新片段参数的最佳实践?

如何更改片段 Kotlin

如何在屏幕方向更改时附加片段?

从广播接收器更改片段