20240514,算法(算数生成,集合)

还有一个大案例,那个就不急了,完结撒花,起码C++是打代码没什么大问题的完结,不像C,还要我返工/笑哭

常用算数生成算法

属于小算法,头文件**#include <numeric>**
accumulate //计算容器累计总和
fill //容器中添加元素

ACCUMULATE

accumulate //计算容器累计总和
accumulate(begin,end,val) /val==累加起始值

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
/*
accumulate(begin,end,val)  //val==起始值
*/
void myprint(int val) {
	cout << val << "  ";
}
void test01() {
	vector<int>v;
	for (int i = 0; i <= 100; i++) {
		v.push_back(i);
	}
	int sum=accumulate(v.begin(), v.end(), 0);
	cout <<sum<< endl;
	sum = accumulate(v.begin(), v.end(), 1000);
	cout << sum << endl;

}
void test02() {
	
}
int main() {
	test01();
	test02();
	return 0;
}
FILL

fill //容器中添加元素 fill(begin,end,val)

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
/*
fill(begin,end,val)  
*/
void myprint(int val) {
	cout << val << "  ";
}
void test01() {
	vector<int>v;
	v.resize(10);
	fill(v.begin(), v.end(), 1000);
	for_each(v.begin(), v.end(), myprint);
	cout << endl;
}
void test02() {
	
}
int main() {
	test01();
	test02();
	return 0;
}

常用集合算法

set_intersection //交集

set_union //并集

set_difference //差集
都必须是有序数列,返回迭代器

SET_INTERECTION

set_intersection //交集 set_intersection(beg1,end1,beg2,end2,iterator dest)返回迭代器

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
//#include<numeric>
using namespace std;
/*
set_intersection(beg1,end1,beg2,end2,iterator dest)
*/
void myprint(int val) {
	cout << val << "  ";
}
void test01() {
	vector<int>v;
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
		v1.push_back(i+5);
	}
	//v2.resize(v.size() < v1.size()?v.size():v1.size());//提前开辟
	v2.resize(min(v.size(), v1.size()));
	vector<int>::iterator iEnd = set_intersection(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
	for_each(v2.begin(), iEnd, myprint);//用返回的迭代器打印输出
}
void test02() {
	
}
int main() {
	test01();
	test02();
	return 0;
}
SET_UNION

set_union //并集 set_intersection(beg1,end1,beg2,end2,iterator dest)

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

void myprint(int val) {
	cout << val << "  ";
}
void test01() {
	vector<int>v;
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
		v1.push_back(i+5);
	}
	v2.resize(v.size()+v1.size());
	vector<int>::iterator iEnd = set_union(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
	for_each(v2.begin(), iEnd, myprint);//用返回的迭代器打印输出
}
void test02() {
	
}
int main() {
	test01();
	test02();
	return 0;
}
SET_DIFFERENCE

set_difference //差集 set_difference (beg1,end1,beg2,end2,iterator dest)

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

void myprint(int val) {
	cout << val << "  ";
}
void test01() {
	vector<int>v;
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
		v1.push_back(i+5);
	}
	v2.resize(v.size());
	vector<int>::iterator iEnd = set_difference(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
	for_each(v2.begin(), iEnd, myprint);//用返回的迭代器打印输出
	cout << endl;


	fill(v2.begin(), v2.end(), 0);
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
	iEnd = set_difference(v1.begin(), v1.end(), v.begin(), v.end(), v2.begin());
	for_each(v2.begin(), iEnd, myprint);//用返回的迭代器打印输出
	cout << endl;
}
void test02() {
	
}
int main() {
	test01();
	test02();
	return 0;
}
相关推荐
绿算技术13 分钟前
Solidigm联合绿算技术共同发布《面向 SOHO AI 推理的存储扩展方案》技术白皮书
人工智能·科技·算法·架构·spark
繁星蓝雨14 分钟前
C++设计原理———重载(extern “C“的由来、顺序依赖、语义依赖、overload、名称修饰符、对象操作、运算符重载、新增运算符、枚举和布尔类型)
c语言·c++·extern c·overload·重载·语义依赖·顺序依赖
handler0118 分钟前
【Linux】虚拟地址空间解析
linux·运维·c++·线程·进程·虚拟地址空间·虚拟地址
(Charon)21 分钟前
【C++】网络缓冲区设计(二):Ring Buffer环形缓冲区、head/tail与跨界读写
开发语言·c++
码匠许师傅31 分钟前
【设计模式精讲】24.观察者模式(Observer)
c++·观察者模式·设计模式·uml
EDPJ44 分钟前
(2026|IPI|我的论文投稿中,PSP 超轻量采样算法,轻量化架构探索/早融合+头压缩)LUMIN:面向工业异常检测的轻量级通用制造检测网络
算法·计算机视觉·架构·异常检测·采样算法
imgsq1 小时前
S-57 数据解剖:把一个 ENC 文件拆给你看
c++·数据可视化
我不会起名字3221 小时前
一天一道算法题(29):单调栈
java·数据结构·python·算法·leetcode·golang·单调栈
迷茫、Peanut1 小时前
中断的时钟
c++
苦瓜小生1 小时前
【前端】【力扣与手撕】十天带你刷完前端算法与手撕,全是最简单好记的最优解法!day4
前端·数据结构·算法·leetcode·面试