我想创建这样的值:(我必须使用以下语法)。所以目标是通过 STL 函数 generate_n 和作为模板函数的仿函数类“Sequencegenerator”创建值。我的问题是,我不知道如何返回值以及如何将其保存到下一次调用。下面是我的代码...非常感谢!(我必须在不使用运算符()的情况下解决它
// Numbers from 1 to 10
generate_n(ostream_iterator<int>(cout, " "), 10, SequenceGenerator<int>(1));
cout << endl;
// Numbers from 10 to 1 (descending order)
generate_n(ostream_iterator<int>(cout, " "), 10, SequenceGenerator<int>(10, -1));
cout << endl;
// Numbers from 0 to 5 (increment 0.5)
generate_n(ostream_iterator<double>(cout, " "), 11, SequenceGenerator<double>(0, 0.5));
cout << endl;
// Letters from A to Z
generate_n(ostream_iterator<char>(cout, ""), 26, SequenceGenerator<char>(’A’));
cout << endl;
我的代码:
#include <algorithm>
#include <iterator>
#include <iostream>
#include "SequenceGenerator.h"
using namespace std;
template <typename T>
class SequenceGenerator : public std::unary_function<T,T>
{
public:
SequenceGenerator(T const start, T const inc):mStart(start),mInc(inc)
{
return mStart = mStart + inc;
}
private:
T const mInc;
static T mStart; //static T mStart
};
int main()
{
generate_n(ostream_iterator<int>(cout, " "), 10, SequenceGenerator<int>(10,1) );
return 0;
}