当前位置: 首页 > news >正文

STL——常用算法(二)

一、常用拷贝和替换算法

1.copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());for_each(v2.begin(), v2.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

2.replace

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}replace(v1.begin(), v1.end(), 5, 10);for_each(v1.begin(), v1.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

3.replace_if

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
class Greater5
{
public:bool operator()(int val){return val >= 5;}
};
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;replace_if(v1.begin(), v1.end(), Greater5(), 10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;
}
int main()
{test01();return 0;
}

4.swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 100);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;swap(v1, v2);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;
}
int main()
{test01();return 0;
}

二、常用算数生成算法

1.accumulate

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i <= 100; i++){v1.push_back(i);}int total = accumulate(v1.begin(), v1.end(), 1000);//5050+1000//for_each(v1.begin(), v1.end(), MyPrint());cout << total << endl;
}
int main()
{test01();return 0;
}

2.fill

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;v1.resize(10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;fill(v1.begin(), v1.end(), 10);for_each(v1.begin(), v1.end(), MyPrint());
}
int main()
{test01();return 0;
}

三、常用集合算法

1.set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(min(v1.size(), v2.size()));vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

2.set_union

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(v1.size() + v2.size());vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

3.set_difference

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(max(v1.size(), v2.size()));vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());for_each(v3.begin(), itEnd1, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}
http://www.lryc.cn/news/383217.html

相关文章:

  • MyCAT 2 底层原理
  • 操作系统实训复习笔记(第7关:生产者消费者问题实践)
  • 通过物联网管理多台MQTT设备-基于全志T527开发板
  • Python学习前简介
  • 【Text2SQL 论文】MAGIC:为 Text2SQL 任务自动生成 self-correction guideline
  • 2024 年 8 款最佳建筑 3D 渲染软件
  • MAB规范(3):Chapter6 Glossary 术语表
  • 40python数据分析numpy基础之diag处理矩阵对角线元素
  • ffmpeg+nginx+video实现rtsp流转hls流,web页面播放
  • 1、Redis系列-Redis高性能原理详解
  • 18.枚举
  • 全省高等职业学校大数据技术专业建设暨专业质量监测研讨活动顺利开展
  • 2-16 基于matlab的动载荷简支梁模态分析程序
  • AI大模型的核心
  • 【Android面试八股文】ViewHolder为什么要被声明成静态内部类?
  • Android 11 系统OTA升级到旧版本(去除升级时间戳校验)
  • 更新表的统计信息并清空缓存--DM8达梦数据库
  • 【前后端实现】AHP权重计算
  • K8S日常运维手册
  • 现在的Java面试都这么扯淡了吗?
  • 安全加固 MariaDB 和 MySQL 数据库
  • 【计算机毕业设计】167校园失物招领微信小程序
  • yum的概念、相关命令、ftp http部署步骤;NFS共享文件操作步骤
  • Spire.PDF for .NET【文档操作】演示:如何删除 PDF 中的图层
  • 【c语言】二级指针
  • 心理健康测试系统设计
  • webcomponents学习
  • 2024会展行业发展趋势预测
  • 达梦(DM8)数据库备份与还原(逻辑备份)二
  • ThreeJS-截屏下载pdf或者图片时白屏