c++的题,求大神解答
1..TheSTLgeneratealgorithmisdefinedasvoidgenerate(ForwardIteratorstart,For-wardIterat...
1..The STL generate algorithm is defined as void generate(ForwardIterator start, For-wardIterator end, NullaryFunction fn) and iterates over the specified range storing the return value of the zero-parameter function fn as it goes. For example, calling generate(v.be-gin(), v.end(), rand) would fill the range [v.begin() to v.end()) with random values.
Write a function FillAscending that accepts an iterator range, then sets the first element in the range to zero, the second to one,etc. Do not use any loops.
2.Write a function StandardDeviation that accepts an input range of iterators over doubles (or values implicitly convertible to doubles) and returns its standard deviation. Do not use any loops – instead use the accumulate function to compute the average, then use accumulate once more to compute the sum.(Hint :To get the number of elements in the range, you can use the distance function)
3. Write a main function to test them.. 展开
Write a function FillAscending that accepts an iterator range, then sets the first element in the range to zero, the second to one,etc. Do not use any loops.
2.Write a function StandardDeviation that accepts an input range of iterators over doubles (or values implicitly convertible to doubles) and returns its standard deviation. Do not use any loops – instead use the accumulate function to compute the average, then use accumulate once more to compute the sum.(Hint :To get the number of elements in the range, you can use the distance function)
3. Write a main function to test them.. 展开
2个回答
展开全部
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
using namespace std;
int FillAscending()
{
static int i = 0;
return i++;
}
// 计算差的平方
class DiffSquare
{
private:
float m_fAvarage;
public:
DiffSquare(float fAvarage) : m_fAvarage(fAvarage){}
float operator()(float fVal1, float fVal2)
{
float fDiff = fVal2 - m_fAvarage;
return fVal1 + fDiff * fDiff;
}
};
// 计算均方差
float StandardDeviation(vector<float>::iterator itBegin, vector<float>::iterator itEnd)
{
int nSize = std::distance(itBegin, itEnd);
// 1 计算平均值
float fAvarage = (float)std::accumulate(itBegin, itEnd, 0.0f) / nSize;
// 2 计算每个元素与均值的差的平方
float f1 = accumulate(itBegin, itEnd, 0.0f, DiffSquare(fAvarage));
return sqrt( f1 / nSize);
}
// 第三问
int main()
{
// 第一问
vector<float> vecFloat(10);
generate(vecFloat.begin(), vecFloat.end(), FillAscending);
// 第二问
float fStanderDeviation = StandardDeviation(vecFloat.begin(), vecFloat.end());
cout << "The StanderDeviation is : " << fStanderDeviation << endl;
return 0;
}
朋友,请【采纳答案】,您的采纳是我答题的动力,如果没有明白,请追问。谢谢。
展开全部
/**************************************************************************
*
* Copyright (c) 2016 www.bjfz.cc. All rights reserved.
*
* @file main.cc
*
* 绠楁硶缁冧范
*
* 1. FillAsscending
* 2. StandardDeviation
*
* @author zhanghb zhaingbo@foxmail.com
*
*************************************************************************/
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cmath>
using namespace std;
struct sq_of_dvi {
sq_of_dvi(double avg){
_avg = avg;
}
double operator()(double y, double x) {
return y + (x-_avg)*(x-_avg);
}
double _avg;
};
int increment()
{
static int i = 0;
return i++;
}
template<typename T>
void FillAsscending(T beg, T end)
{
generate(beg, end, increment);
}
template<typename T>
double StandardDeviation(T beg, T end)
{
// 1. calc the avg
double avg = accumulate(beg, end, 0.0) / (end - beg);
// 2. calc the sum
// 2.1 squares_of_deviations
struct sq_of_dvi sod(avg);
// calc the sum of squares_of_deviations
double sum = accumulate(beg, end, 0.0, sod);
// 3. standard deviation
return sqrt(sum);
}
int main(int argc, char *argv[])
{
vector<int> ivec(10);
vector<int>::iterator beg;
beg = ivec.begin();
while (beg != ivec.end()) {
std::cout << *beg++ << "\t";
}
std::cout << std::endl;
FillAsscending(ivec.begin(), ivec.end());
beg = ivec.begin();
while (beg != ivec.end()) {
std::cout << *beg++ << "\t";
}
std::cout << std::endl;
std::cout << StandardDeviation(ivec.begin(), ivec.end()) << std::endl;
return 0;
}
【认真回答,呈请采纳】
欲发红包,请私聊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询