• 【C++】常用算术生成算法


    0.前言

    在这里插入图片描述

    1.accumulate

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    
    // 常用算术生成算法 
    #include
    #include //accumulate 的调用头文件
    
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i <= 100; i++)
    	{
    		v.push_back(i);
    	}
    
    	int total = accumulate(v.begin(), v.end(), 10000); //10000 是初始值
    
    	cout << "Total = " << total << endl;
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

    2.fill

    在这里插入图片描述

    #include 
    using namespace std;
    
    
    // 常用算术生成算法 fill
    #include
    #include  //fill 的头文件
    #include //for_each 的头文件
    
    void myPrint(int val)
    {
    	cout << val << " ";
    }
    
    void test01()
    {
    	vector<int>v;
    	v.resize(10);
    
    	cout << "填充前:" << endl;
    	for_each(v.begin(), v.end(), myPrint);
    	cout << endl;
    
    	fill(v.begin(), v.end(), 100);
    	cout << "填充后:" << endl;
    	for_each(v.begin(), v.end(), myPrint);
    	cout << endl;
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

  • 相关阅读:
    详解vue3的ref和reactive
    边缘计算网关
    Kubernetes 加入主节点报错
    [数据结构] 二叉树--堆
    如何解hard算法题?
    抖音web版地址个人主页和视频地址
    node.js原生模块
    AWS清除CloudFront缓存
    LVS----DR模式
    VHOST-SCSI代码分析(1)VHOST SCSI设备模拟
  • 原文地址:https://blog.csdn.net/m0_48808835/article/details/132782318