C++头文件algorithm中的函数功能详解

yizhihongxing

接下来我会为您详细讲解 “C++头文件algorithm中的函数功能详解”的攻略。

1. 简介

C++ STL (Standard Template Library) 库提供了很多强大的功能, algorithm 是其中的一个头文件,提供了 许多算法、排序、搜索 和数值处理功能。

2. 常用函数

2.1 排序算法

2.1.1 std::sort

template< class RandomIt>
void sort( RandomIt first, RandomIt last );
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

sort() 函数用于对序列进行升序排序,默认从小到大排序。比如对数组进行排序:

#include <iostream>
#include <algorithm> 

int main() 
{
    int a[] = {2, 1, 4, 5, 3};
    int n = sizeof(a)/sizeof(a[0]);

    std::sort(a, a+n);

    for (int i=0; i<n; i++) 
        std::cout << a[i] << " ";
}

输出结果为:

1 2 3 4 5 

也可以通过comp函数指定排序规则:

bool cmp(int x,int y){
    return x>y;
}

int main() 
{
    int a[] = {2, 1, 4, 5, 3};
    int n = sizeof(a)/sizeof(a[0]);

    std::sort(a, a+n, cmp); // 降序排序

    for (int i=0; i<n; i++) 
        std::cout << a[i] << " ";
}

输出结果为:

5 4 3 2 1 

2.1.2 std::nth_element

template< class RandomIt >
void nth_element( RandomIt first, RandomIt nth, RandomIt last );
template< class RandomIt, class Compare >
void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp );

std::nth_element 函数用于查找序列的第 n 个元素,并将第 n 个元素放到正确的位置,其余的元素在其原有位置。比如:

#include <iostream>
#include <algorithm> 

int main() 
{
    int a[] = {2, 1, 4, 5, 3};
    int n = sizeof(a)/sizeof(a[0]);

    std::nth_element(a, a+2, a+n);

    for (int i=0; i<n; i++) 
        std::cout << a[i] << " ";
}

输出结果为:

2 1 3 5 4 

2.2 查找算法

2.2.1 std::find

template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );

std::find 函数用于在序列中查找某个元素,如果找到了则返回该元素所在的位置(迭代器),否则返回末尾迭代器。比如:

#include <iostream>
#include <algorithm> 

int main()
{
    int a[] = {2, 1, 4, 5, 3};
    int n = sizeof(a)/sizeof(a[0]);
    int* p;
    p = std::find(a, a+n, 4);
    if (p != a+n)
        std::cout << "find 4 at position " << (p-a+1) << "\n";
    else
        std::cout << "4 not found\n";
}

输出结果为:

find 4 at position 3 

2.2.2 std::binary_search

template< class ForwardIt, class T >
bool binary_search( ForwardIt first, ForwardIt last, const T& value );
template< class ForwardIt, class T, class Compare >
bool binary_search( ForwardIt first, ForwardIt last, const T& value, Compare comp );

std::binary_search 函数用于在有序序列中查找某个元素,如果找到了则返回 true,否则返回 false。比如:

#include <iostream>
#include <algorithm> 

int main()
{
    int a[] = {1, 2, 3, 4, 5};
    int n = sizeof(a)/sizeof(a[0]);

    if (std::binary_search(a, a+n, 4))
        std::cout << "4 found\n";
    else
        std::cout << "4 not found\n";
}

输出结果为:

4 found 

3. 总结

以上就是本篇攻略的全部内容,介绍了一些 algorithm 头文件中的常用函数,其中包括了排序算法和查找算法两个方面。相信通过本篇攻略,您已经了解了 algorithm 头文件的相关知识,并且能够熟练使用其提供的功能。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++头文件algorithm中的函数功能详解 - Python技术站

(0)
上一篇 2023年6月27日
下一篇 2023年6月27日

相关文章

  • Mysql 实现字段拼接的三个函数

    要实现MySQL的字段拼接,可以使用以下三个函数: CONCAT CONCAT_WS GROUP_CONCAT 1. CONCAT函数 CONCAT 函数实现了两个或多个字符串的拼接。 语法: CONCAT(string1,string2,…,stringN) 示例: SELECT CONCAT(‘Hello’, ‘ ‘, ‘world’) AS res…

    other 2023年6月25日
    00
  • kafka常用命令合集

    以下是“kafka常用命令合集”的完整攻略: kafka常用命令合集 Kafka是一个分布式的消息队列系统,常用于大规模数据处理和实时数据流处理。本攻略将详细讲解Kafka常用命令,包括创建主题、发送消息、消费消息等内容。 创建主题 在Kafka中,主题是消息的逻辑分类,可以通过以下命令创建主题: bin/kafka-topics.sh –create -…

    other 2023年5月8日
    00
  • 详解C++ string常用截取字符串方法

    详解C++ string常用截取字符串方法 在C++中,string类型是一个非常常用的数据类型,它可以存储字符串并提供一系列字符串处理的方法。其中,截取字符串是string的常见操作之一。下面是C++ string常用的截取字符串方法: 方法一:使用substr函数 substr函数可以截取字符串中的任意一段子串,其参数为子串截取的开始位置和长度,其函数原…

    other 2023年6月20日
    00
  • python循环嵌套的多种使用方法解析

    Python循环嵌套的多种使用方法解析 循环嵌套是指在一个循环体内部再嵌套另一个循环体。Python中的循环嵌套可以用于处理复杂的问题,提供了更灵活的控制流程。本文将详细介绍Python循环嵌套的多种使用方法,并提供两个示例说明。 1. 嵌套循环的基本语法 Python中的嵌套循环可以使用for循环或while循环来实现。基本语法如下: for 变量1 in…

    other 2023年7月27日
    00
  • Windows系统查看本机IP、网关地址的2个方法图文教程

    当你想要查看Windows系统中本机的IP地址和网关地址时,有两种方法可以实现。下面是详细的攻略: 方法一:使用命令提示符(Command Prompt) 打开开始菜单,搜索并打开“命令提示符”(或者按下Win键+R,输入“cmd”并按下回车键)。 在命令提示符窗口中,输入以下命令并按下回车键:ipconfig。 系统将显示出本机的网络配置信息,包括IP地址…

    other 2023年7月30日
    00
  • 使用top命令分析linux系统性能的详解

    使用top命令分析linux系统性能的详解 在Linux系统中,top是最常用的性能监控命令之一。它可以实时显示系统资源的使用情况,包括CPU、内存、进程等。下面是使用top命令分析Linux系统性能的详解。 使用top命令 在Linux系统中,可以通过以下命令打开top: top 或者 top -d <秒数> 其中,-d参数指定top命令的更新…

    other 2023年6月27日
    00
  • WAC集中转发部署

    多线程CSerialPort类的多串口通信实现的完整攻略 CSerialPort是一个用于串口通信的C++类库,可以在Windows和Linux等操作系统上使用。本文将为您提供使用多线程CSerialPort类实现多串口通信的完整攻略,并提供两个示例说明。 步骤1:创建CSerialPort对象 在使用CSerialPort进行串口通信时,首先需要创建CSe…

    other 2023年5月5日
    00
  • sqlalchemy转json的几种常用方式

    SQLAlchemy是一个流行的Python ORM库,它可以将Python对象映射到关系数据库中的表。在使用SQLAlchemy时,有时需要将查询结果转换为JSON格式。以下是SQLAlchemy转JSON的几种常用方式的完整攻略,包含两个示例说明。 方式一:使用json.dumps() Python的json模块提供了一个dumps()函数,可以将Pyt…

    other 2023年5月9日
    00
合作推广
合作推广
分享本页
返回顶部