STL-常用算法(一.遍历 查找 排序)

2023-11-09

目录

常用遍历算法:

for_each和transform函数示例:

 常用查找算法:

find函数示例:

find_if函数示例:

adjacent_find示例:

binary_search函数示例:

count函数示例:

count_if函数示例:

常用排序算法:

sort函数示例:

random_shuffle函数示例:

merge函数示例:

reverse函数示例: 


常用遍历算法:

for_each    //遍历容器
transform   //搬运容器到另一个容器中

for_each和transform函数示例:

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

//普通函数
void print01(int val)
{
	cout << val << " ";
}
//仿函数
class print02
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

//for_each算法基本用法
void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//遍历算法
	for_each(v.begin(), v.end(), print01);
	cout << endl;

	for_each(v.begin(), v.end(), print02());
	cout << endl;
}


class TransForm
{
public:
	int operator()(int val)
	{
		return val;
	}

};

//仿函数
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test02()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>vTarget; //目标容器

	vTarget.resize(v.size()); // 目标容器需要提前开辟空间

	transform(v.begin(), v.end(), vTarget.begin(), TransForm());

	for_each(vTarget.begin(), vTarget.end(), print02());
	cout << endl;
}

int main() {

	test01();
	test02();
	system("pause");

	return 0;
}

运行结果:

 常用查找算法:

find             //查找元素
find_if          //按条件查找元素
adjacent_find    //查找相邻重复元素
binary_search    //二分查找法
count            //统计元素个数
count_if         //按条件统计元素个数

find函数示例:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}
	//查找容器中是否有 5 这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到:" << *it << endl;
	}
}

class Person {
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	//重载==
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}

public:
	string m_Name;
	int m_Age;
};

void test02() {

	vector<Person> v;

	//创建数据
	Person p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 40);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator it = find(v.begin(), v.end(), p2);
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
	}
}

int main() {

	test01();

	test02();

	system("pause");

	return 0;
}

运行结果:

find_if函数示例:

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

class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到大于5的数字:" << *it << endl;
	}
}

//自定义数据类型
class Person {
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}

};

void test02() {

	vector<Person> v;

	//创建数据
	Person p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 40);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
	}
}

int main() 
{

	test01();

	test02();

	system("pause");

	return 0;
}

 运行结果:

 adjacent_find示例:

void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(4);
	v.push_back(3);

	//查找相邻重复元素
	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end()) {
		cout << "找不到!" << endl;
	}
	else {
		cout << "找到相邻重复元素为:" << *it << endl;
	}
}

运行结果:

 binary_search函数示例:

void test01()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//二分查找
	bool ret = binary_search(v.begin(), v.end(),2);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

运行结果:

 

count函数示例:

void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);

	int num = count(v.begin(), v.end(), 4);

	cout << "4的个数为: " << num << endl;
}

运行结果:

 

count_if函数示例:

class Greater4
{
public:
	bool operator()(int val)
	{
		return val >= 4;
	}
};

//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);

	int num = count_if(v.begin(), v.end(), Greater4());

	cout << "大于4的个数为: " << num << endl;
}

运行结果:

 

常用排序算法:

sort            //对容器内元素进行排序
random_shuffle  //洗牌   指定范围内的元素随机调整次序
merge           // 容器元素合并,并存储到另一容器中
reverse         // 反转指定范围的元素

sort函数示例:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01() {
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);

	//sort默认从小到大排序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//从大到小排序
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

random_shuffle函数示例:

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	srand((unsigned int)time(NULL));
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//打乱顺序
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

运行结果:

 

merge函数示例:

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 + 1);
	}

	vector<int> vtarget;
	//目标容器需要提前开辟空间
	vtarget.resize(v1.size() + v2.size());
	//合并  需要两个有序序列
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
	for_each(vtarget.begin(), vtarget.end(), myPrint());
	cout << endl;
}

运行结果:

reverse函数示例: 

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(2);
	v.push_back(8);

	cout << "反转前: " << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	cout << "反转后: " << endl;

	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

运行结果:

 总结:

        本文介绍STL中常用的遍历,查找,排序算法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

STL-常用算法(一.遍历 查找 排序) 的相关文章

随机推荐

  • SQL Server主流版本生命周期管理

    SQL Server 生命周期 每个版本的 SQL Server 都有至少 10 年的支持期限 其中包括五年的主要支持和五年的扩展支持 主要支持 包括功能 性能 可伸缩性和安全更新 扩展支持 仅包含安全更新 终止支持 有时也称为生命周期结束
  • 2020年高教社杯全国大学生数学建模竞赛赛题 C题分析与思路!(持续更新)

    C题 中小微企业的信贷决策 1 C题题目背景 分析 在实际中 由于中小微企业规模相对较小 也缺少抵押资产 因此银行通常是依据信贷政策 企业的交易票据信息和上下游企业的影响力 向实力强 供求关系稳定的企业提供贷款 并可以对信誉高 信贷风险小的
  • 【nginx编译-zierror: ‘struct crypt_data‘ has no member named ‘current_salt‘】

    nginx编译 src os unix ngx user c In function ngx libc crypt src os unix ngx user c 26 7 error struct crypt data has no mem
  • C#中Class与Struct区别

    C 中Class与Struct区别 1 class 是引用类型 继承自System Object stuce是值类型 继承自System ValueType类 因此不具多态性 但是注意 System ValueType是个引用类型 2 从职
  • 如何用SPSS对数据进行标准化处理?

    SPSS统计分析软件是我最早接触的数据分析工具 我的博客将陆续介绍SPSS统计分析软件的相关内容 这类文章将统一按照在标题或者正文第一段出现 SPSS案例分析 编号 的形式组织 便于读者朋友们快速查询 收集 今天是第一篇 即 SPSS案例分
  • python循环与文件操作

    if 语句语法结构 if 条件 elif 条件 else 1 如果表达式的值 非0 或者为布尔值 True 则代码组 if suite 被执行 否则就去执行 else suite 2 只要表达式数字为 非零值 即为 True 3 空字符串
  • 移植使用tslib 库

    目录 tslib 简介 tslib 移植 下载tslib 源码 编译tslib 源码 tslib 安装目录下的文件夹介绍 在开发板上测试tslib tslib 库函数介绍 打开触摸屏设备 配置触摸屏设备 读取触摸屏数据 基于tslib 编写
  • STL库的使用之容器模板类QVector使用

    Qt中提供了一组通用的基于模板的容器类 对比C 中的STL库的容器类 Qt的这些容器类更轻量 更安全并且容易使用同时在速度 内存消耗 内联代码等方面进行了优化 存储在Qt的容器中数据必须是可赋值的数据类型 数据类型必须提供一个默认的构造函数
  • BES2300X,BES2500X——UI(按键,提示音,指示灯)

    本文是BES2300X BES2500X系列博文UI部分 一个耳机 音箱 UI是联系使用者与开发者最直接的一个窗口 当然 对于吾等码农而言 UI设计并不是我们最关心的 我们主要做的是UI实现 本文 写BES最基础UI 按键 指示灯 提示音
  • 基于Qt的收银点餐系统之UI的改进——QStackedLayout和QScrollArea的使用

    待解决问题 在收银点餐系统之UI的基本实现中 我们实现了本系统中最基本的UI 这一个UI是静态的 不能够动态添加按钮 关于如何添加见参考资料 也不能实现点击不同的分类 出现不同的界面等 前者的逻辑通过代码很好实现 故不赘述 后者则需要用到一
  • 力扣第45天----第392题、第115题

    力扣第45天 第392题 第115题 文章目录 一 第392题 判断子序列 二 第115题 不同的子序列 一 第392题 判断子序列 挺简单的 思路跟以前的都差不多 class Solution public bool isSubseque
  • Go解析yaml和yml文件

    Go解析yaml和yml文件 文章目录 Go解析yaml和yml文件 1 yaml概述 2 功能 3 示例 4 语言的构成元素 1 清单 数组 2 关系数组 3 区块的字符 4 保留换行 Newlines preserved 5 折叠换行
  • 英文期刊催稿信模板_SCI投稿委婉催稿信模板

    SCI投稿委婉催稿信模板1 Dear editor I m not sure if it is the right time to contact you to inquire about the status of my submitte
  • 34种ArcGIS常用操作技巧大汇总

    概述 ArcGIS产品线为用户提供一个可伸缩的 全面的GIS平台 ArcObjects包含了许多的可编程组件 从细粒度的对象 例如单个的几何对象 到粗粒度的对象 例如与现有ArcMap文档交互的地图对象 涉及面极广 这些对象为开发者集成了全
  • Burpsuite2022.1详细安装步骤包含证书安装

    burpsuite安装 burpsuite2022 1 https pan baidu com s 1k46tVXOKfdSwxOha UNeyQ 提取码 6954 将 burp suite 压缩包解压到英文目录下 这里我解压到了 E Bu
  • matlab图像滤波

    转自 http hi baidu com wang 5Fpw blog item 36354a637ac87b48eaf8f879 html clc clear all I imread eight tif 用中值滤波 多维滤波 使用中心为
  • zipkin学习--01--理论

    一 介绍 是分布式跟踪系统 Distributed Tracking System 监控微服务各个服务的调用情况 举例 一个请求A 需要先后调用f1 f2 f3等微服务单元的接口 我们可以通过链路追踪查看f1 f2 f3对应接口的耗时 主要
  • CLIP:Contrastive Language-Image Pre-Training

    参考博客 CLIP论文阅读 Learning Transferable Visual Models From Natural Language Supervision CLIP Connecting Text and Images 引言 在
  • Imageview 图片的自动适配 Togglebutton 点击事件

    Imageview 图片控件 主要属性src background 图片的自动适配 Android开发图片https www easyicon net iconsearch android 素材网址 Togglebutton 开合关的两种状
  • STL-常用算法(一.遍历 查找 排序)

    目录 常用遍历算法 for each和transform函数示例 常用查找算法 find函数示例 find if函数示例 adjacent find示例 binary search函数示例 count函数示例 count if函数示例 常用