STL:generate()和generate_n()
generate()功能:用指定函数对象产生的值去给容器指定范围内元素赋值templatevoid generate(ForwardIterator _First,ForwardIterator _Last,Generator _Gen);generate_n()功能:一个函数对象产生的值给一定的范围内指定数目的容器元
·
generate()
功能:用指定函数对象产生的值去给容器指定范围内元素赋值
template<class ForwardIterator, class Generator>
void generate(
ForwardIterator _First,
ForwardIterator _Last,
Generator _Gen
);
generate_n()
功能:一个函数对象产生的值给一定的范围内指定数目的容器元素赋值
template<class OutputIterator, class Diff, class Generator>
void generate_n(
OutputIterator _First,
Diff _Count,
Generator _Gen
);
example:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class IntSequence
{
private:
int m_Value;
public:
IntSequence(int value):m_Value(value){}
int operator()()
{
return m_Value++;
}
};
int main()
{
vector<int> IntVec;
generate_n(back_inserter(IntVec),9,IntSequence(1));
for(vector<int>::iterator it=IntVec.begin();it!=IntVec.end();++it)
{
cout<<*it<<" ";
}
cout<<endl;
generate(++IntVec.begin(),--IntVec.end(),IntSequence(10));
for(vector<int>::iterator it=IntVec.begin();it!=IntVec.end();++it)
{
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
结果:
1,2,3,4,5,6,7,8,9
1,10,11,12,13,14,15,16,9
更多推荐
已为社区贡献1条内容
所有评论(0)