IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> STL中的常用算术生成算法 -> 正文阅读

[C++知识库]STL中的常用算术生成算法

常用算术生成算法

学习目标:

  • 掌握常用的算术生成算法

注意:

  • 算术生成算法属于小型算法,使用时包含的头文件为 #include <numeric>

算法简介:

  • accumulate // 计算容器元素累计总和

  • fill // 向容器中添加元素




1. accumulate

功能描述:

  • 计算区间内 容器内各元素累加总和

函数原型:

  • accumulate(iterator beg, iterator end, value); 返回一个int型变量

    // 计算容器元素累计总和

    // beg 开始迭代器

    // end 结束迭代器

    // value 起始累加值。一般都是从0开始累加

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>



void test01()
{
	cout << "针对内置数据类型" << endl;
	vector<int> v1;
	for (int i = 1; i <=100; i++) {
		v1.push_back(i);
	}

	// 不用accumulate算法,得自己写个for循环去统计容器内各元素累加和
	cout << "如果不用accumulate算法,得自己写个for循环去统计容器内各元素累加和" << endl;
	int normal_sum1 = 0;
	for (int i = 0; i < 100; i++) {
		normal_sum1 = normal_sum1 + v1[i];
	}
	cout <<"normal_sum1 =" << normal_sum1 << endl<<endl<<endl;

	int accumulate_sum1 = accumulate(v1.begin(), v1.end(), 0);
	cout << "运用accumulate算法直接一步到位:" << endl;
	cout << "accumulate_sum1="<< accumulate_sum1 << endl;
}

// 针对自定义数据类型
class Person {
public:
	Person(string name, int age) :m_Name(name), m_Age(age) {

	}
	



	int m_Age;
	string m_Name;



};



int main() {
	test01();
	
	cout << endl << endl << endl;
	system("pause");
	return 0;


}

在这里插入图片描述

总结 :accumulate使用时头文件注意是 numeric,这个算法很实用

由于所学知识有限,暂时不知道accumulate算法针对自定义数据类型怎么写






2. fill

功能描述:

  • 将容器区间内元素填充为 指定的值

函数原型:

  • fill(iterator beg, iterator end, value);

    // 向容器中填充元素

    // beg 开始迭代器

    // end 结束迭代器

    // value 填充的值




#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>



void Myprint1(int n) {

	cout << n << "    ";

}
bool Mycompare1(int a, int b) {

	return a > b;
}

class Myreplace1 {
public:
	bool operator()(int n) {

		return n > 66;
	}


};
class Myreplace2 {
public:
	bool operator()(int n) {

		return n < 10;
	}


};

void test01()
{
	cout << "针对内置数据类型" << endl;
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(201);
	v1.push_back(3);
	v1.push_back(666);
	v1.push_back(58);
	v1.push_back(79);
	v1.push_back(67);
	v1.push_back(6);
	v1.push_back(6);
	cout << "v1里的元素如下:" << endl;

	for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;


	cout << "将[v1.begin()到v1.end()-1)这个区间里的元素改为666" << endl;
	fill(v1.begin(), v1.end() - 1, 666);

		for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;

		cout << "将[v1.begin()到v1.end()-3]这个区间里的元素改为888" << endl;
		fill(v1.begin(), v1.end()-3, 888);

		for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
}

// 针对自定义数据类型
class Person {
public:
	Person(string name, int age) :m_Name(name), m_Age(age) {

	}
	

	int m_Age;
	string m_Name;



};




void Myprint2(Person p) {
	cout << "姓名:" << p.m_Name << "    "
		<< "年龄:" << p.m_Age << endl;
}




void test02() {
	cout << "针对自定义数据类型" << endl;
	vector<Person> v2;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 5);
	Person p3("ccc", 60);
	Person p4("ddd", 30);
	Person p5("grep", 3);
	Person p6("awk", 200);
	Person p7("sed", 105);

	Person p8("Shell", 3060);
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);
	v2.push_back(p6);


	cout << "v2里的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl ;
	cout << "p7的姓名:" << p7.m_Name << "    "
		<< "p7的年龄:" << p7.m_Age << endl << endl;

	cout << "先将[v2.begin(),v2.end()-2)这个区间里的对象全改为p7" << endl;
	fill(v2.begin(), v2.end() - 2, p7);
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;
	cout << "p3的姓名:" << p3.m_Name << "    "
		<< "p3的年龄:" << p3.m_Age << endl << endl;
	cout << "再将[v2.begin(),v2.end()-1)这个区间里的对象全改为p3" << endl;
	fill(v2.begin(), v2.end() - 1, p3);
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;


}





int main() {
	test01();
	test02();
	cout << endl << endl << endl;
	system("pause");
	return 0;


}

在这里插入图片描述
总结:利用fill可以将容器区间内元素填充为 指定的值







  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-25 23:05:06  更:2022-09-25 23:06:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 3:16:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码