1、generate() 和 generate_n()使用方法
可以理解为一种填充元素的方法:
demo源码实现:
#include <iostream>
#include <vector> //动态数组
#include <deque> //双端队列
#include <algorithm>
#include <numeric>
#include <time.h>
using namespace std;
void showArr(vector<int> Arr)
{
vector<int>::iterator iteArr = Arr.begin();
for (iteArr ; iteArr != Arr.end(); iteArr++)
{
cout << *iteArr << "\t";
}
cout << endl;
}
vector<int> createStaticArr()
{
vector<int> vectorInt;
vectorInt.push_back(1);
vectorInt.push_back(2);
vectorInt.push_back(3);
vectorInt.push_back(4);
vectorInt.push_back(5);
vectorInt.push_back(6);
vectorInt.push_back(7);
vectorInt.push_back(8);
showArr(vectorInt);
return vectorInt; //返回数组
}
int main()
{
vector<int> arr1 = createStaticArr();
cout << endl;
generate(arr1.begin(), arr1.begin() + 5, []() {return 4; });//从开始之后的5个位置数都是4
showArr(arr1);
cout << endl;
generate_n(arr1.begin(), 2, []() {return 2; });//在以上基础上同样使用方法 迭代器到2的位置
showArr(arr1);
cout << endl;
return 0;
}
执行结果:
1 2 3 4 5 6 7 8
4 4 4 4 4 6 7 8
2 2 4 4 4 6 7 8
2、fill填充器
fill()函数也可以用于填充容器中某个范围内的值。它接受两个开始和结束的迭代器,并在容器中填充一个值,该值从begin所指向的位置开始,并在end所指向的位置之前。
用法:
void fill (iterator begin, iterator end, type value);
参数:
begin:该函数将从迭代器begin指向的位置开始填充值。
end:该函数将值填充到迭代器末端指向的位置之前的位置。
value:此参数表示容器中的函数要填充的值。
注意:请注意,范围中包括“开始”,但不包括“结束”。
返回值:此函数不返回任何值。
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vect(8);
// calling fill to initialize values in the
// range to 4
for (int i = 0; i < vect.size(); i++)
cout <<"处理前:"<<vect[i] << " ";
cout << "\n";
fill(vect.begin() + 2, vect.end() - 1, 4);
for (int i = 0; i < vect.size(); i++)
cout <<"处理后:"<< vect[i] << " ";
cout << "\n";
// Filling the complete vector with value 10
// fill(vect.begin(), vect.end(), 10);
// cout << endl;
// for (int i = 0; i < vect.size(); i++)
// cout << vect[i] << " ";
return 0;
}
执行结果:
处理前:0 处理前:0 处理前:0 处理前:0 处理前:0 处理前:0 处理前:0 处理前:0
处理后:0 处理后:0 处理后:4 处理后:4 处理后:4 处理后:4 处理后:4 处理后:0
其他:fill_n函数的作用是:给你一个起始点,然后再给你一个数值count和val。把从起始点开始依次赋予count个元素val的值。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
//定义迭代器
vector<int> vec (8);
//输出覆盖前的迭代器
cout << "fill前:" << " "<< ends;
for (int i = 0; i < 8; i++) {
cout << vec[i] << " " << ends;
}
cout << endl;
fill(vec.begin(), vec.begin() + 3, 5);
fill_n(vec.begin() + 4, 2,9); //从位置4开始后两个都是9
//输出覆盖后的迭代器
cout << "fill后:" << " " << ends;
for (int i = 0; i< 8; i++)
{
cout << vec[i] << " " << ends;
}
cout << endl;
return 0;
}
执行结果:
fill前: 0 0 0 0 0 0 0 0
fill后: 5 5 5 0 9 9 0 0
3、结合上述及标题
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std;
bool fun2(int x)
{
return x % 2 == 0;
}
int main()
{
int num = 10;
vector<int> vec0(num);
vector<int> vec(num);
srand((unsigned)time(NULL));//随机撒数,如果不使用这个,则每次生成的结果都一样
generate(vec0.begin(),vec0.end(),rand);
// generate()的用法,该函数接受一个区间,由前两个参数指定,并将区间中的每个元素设置为第三个参数返回的值
for(int i = 0;i < vec0.size();i++)
{
vec[i]=vec0[i]/100000000;
cout << vec[i] << " ";
}
cout << endl;
//count_if可以统计容器中满足特定条件的元素的个数
int count = count_if(vec.begin(),vec.end(),fun2);//满足函数fun2的条件
cout <<"---->" <<count << endl;
return 0;
}
随机执行结果:
10 6 17 11 6 12 9 11 18 0
---->6
参考
https://www.learnfk.com/c++/cpp-algorithm-generate-function.html
https://www.geeksforgeeks.org/stdgenerate-in-c/
http://c.biancheng.net/view/620.html
https://vimsky.com/zh-tw/examples/usage/fill-function-in-c-stl-with-examples.html