C++使用技巧(二十五):srand、rand、fill/fill_n、generate/generate_n、count_if搭配使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`std::mt19937_64` 和 `std::srand()` 都是用于生成随机数的函数/类,但它们在实现机制和使用方式上有一些区别。下面是它们的优缺点对比: std::srand(): - 实现机制:`std::srand()` 是 C++ 标准库中的一个伪随机数生成函数,它使用线性同余算法生成伪随机数。它接受一个整数参数作为种子,用于初始化随机数生成器。 - 优点: - 简单易用,只需设置种子即可生成伪随机数。 - 在旧版本的 C++ 标准中是唯一的随机数生成函数。 - 缺点: - 生成的随机数质量较低,存在一定的规律性。 - 由于使用全局状态来保存随机数种子,不适合多线程环境。 std::mt19937_64: - 实现机制:`std::mt19937_64` 是 C++11 引入的一个伪随机数生成器类,它使用 Mersenne Twister 算法生成高质量的伪随机数。它接受一个 64 位整数作为种子,用于初始化随机数生成器。 - 优点: - 生成的随机数质量较高,具有良好的统计特性,更难预测和破解。 - 可以通过创建多个 `std::mt19937_64` 实例来支持多线程环境。 - 缺点: - 相对于 `std::srand()` 来说,使用方式稍微复杂一些,需要创建随机数生成器对象,并设置种子。 综上所述,如果你的项目使用 C++11 或更高版本的编译器,推荐使用 `std::mt19937_64` 来生成随机数,因为它生成的随机数质量更高。而如果你在旧版本的 C++ 中,可以使用 `std::srand()` 来生成随机数,但需要注意其随机数质量较低。另外,对于多线程环境,`std::mt19937_64` 更适合使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值