蓝桥杯C++竞赛常用库函数介绍

本文详细介绍了C++中二分查找、最值查找(min,max,min_element,max_element,nth_element)、排序(sort,自定义比较),全排列(next_permutation,prev_permutation)以及大小写处理和一些其他库函数,如内存设置、交换、反转和去重,旨在帮助准备蓝桥杯竞赛的选手熟悉常用算法和函数应用。
摘要由CSDN通过智能技术生成


前言

最近开始准备蓝桥杯C++组的比赛了,是在自己学习过程中的记录,也分享给大家! 一些蓝桥杯C++竞赛常用库函数!


一、二分查找

1. 二分查找的前提

1.数只能对数组进行二分查找
2.这个数组中的元素只能是单调
3.一般是单调递减,单调递增也行(需修改比较函数)

2.binary_search函数

std::binary_search 函数定义在 头文件中,用于在已排序的序列(数组或容器)中查找特定的元素,通过二分查找来确定序列中是否存在目标元素,返回bool值
二分查找的时间复杂度为 O(log n),其中 n 是序列的大小。这是因为在每一次比较中,二分查找将搜索范围减半,直到找到目标值或者搜索范围为空为止。

//使用例程
#include <iostream>
#include <vector>
#include <algorithm> // 包含 <algorithm> 头文件

int main() {
    std::vector<int> vec = {1, 3, 5, 7, 9};

    // 在有序序列中查找目标值 5
    if (std::binary_search(vec.begin(), vec.end(), 5)) {
        std::cout << "目标值 5 存在于序列中" << std::endl;
    } else {
        std::cout << "目标值 5 不存在于序列中" << std::endl;
    }

    return 0;
}

3.lower_bound函数和upper_bound函数

lower_bound(st,ed,x)返回地址[st,ed)中第一个大于等于x的元素的地址(迭代器)。
upper_bound(st,ed,x)返回地址[st,ed)中第一个大于x的元素的地址(迭代器)。

//例子
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 2, 3, 3, 3, 5};

    // 使用 lower_bound 查找第一个大于等于 3 的位置
    auto lower = std::lower_bound(vec.begin(), vec.end(), 3);
    std::cout << "lower_bound: " << std::distance(vec.begin(), lower) << std::endl;
	//std::distance(vec.begin(), lower) 是一个 C++ 标准库中的算法函数
	//用于计算两个迭代器之间的距离
	//vec.begin() 是容器 vec 的起始迭代器
	//lower 是 std::lower_bound 函数返回的迭代器,指向第一个大于等于目标值的位置。
	//等价于lower - vec.begin() 
	
    // 使用 upper_bound 查找第一个大于 3 的位置
    auto upper = std::upper_bound(vec.begin(), vec.end(), 3);
    std::cout << "upper_bound: " << std::distance(vec.begin(), upper) << std::endl;

    return 0;
}

4.蓝桥杯例题

在这里插入图片描述

二、最值查找

1. min和max函数

std::min 和 std::max 是 C++ 标准库中的两个算法函数,用于返回两个值中的最小值和最大值。它们在 <algorithm> 头文件中声明。时间复杂度为 O(n)

#include <iostream>
#include <algorithm>

int main() {
    int a = 10;
    int b = 20;
 

    // 返回两个值中的较小值
    int min_val = std::min(a, b);
    std::cout << "最小值:" << min_val << std::endl;

    // 返回两个值中的较大值
    int max_val = std::max(a, b);
    std::cout << "最大值:" << max_val << std::endl;
	
	//也可以返回一个列表中最大最小的值,用大括号{}括起来
	std::cout << "列表中的最大值:" << std::max({1, 2, 3, 4, 5}) << std::endl;

    return 0;
}

2.min_element和max_element函数

std::min_element 和 std::max_element 是 C++ 标准库中的两个算法函数,用于在容器中查找最小值和最大值对应的迭代器。它们在 <algorithm> 头文件中声明。时间复杂度为 O(n)
注意它返回的是迭代器,要的到返回的值需要*()

std::min_element(first, last)
std::max_element(first, last)
first 和last:表示要查找的范围的起始和结束迭代器(包含 first,但不包含 last)。[first, last)

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {3, 1, 4, 1, 5, 9, 2, 6};

    // 返回容器中的最小值对应的迭代器
    auto min_it = std::min_element(vec.begin(), vec.end());
    std::cout << "最小值:" << *min_it << std::endl;

    // 返回容器中的最大值对应的迭代器
    auto max_it = std::max_element(vec.begin(), vec.end());
    std::cout << "最大值:" << *max_it << std::endl;

    return 0;
}

3.nth_element函数

std::nth_element 是 C++ 标准库中的一个算法函数,用于对容器中的元素进行部分排序,使得指定位置的元素处于排序后的正确位置。它在 <algorithm> 头文件中声明。

std::nth_element (first, nth, last)
std::nth_element 函数对范围 [first, last) 中的元素进行部分排序,使得迭代器 nth 指向的元素处于排序后的正确位置。
换句话说,nth 之前的元素都不大于 nth位置的元素,nth 之后的元素都不小于 nth 位置的元素。但是,nth 位置的元素不一定是整个范围中的第 nth 小的元素。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {3, 1, 4, 1, 5, 9, 2, 6};

    // 将容器中的第5个元素置于正确的位置 ,排序后的容器第五个是正确位置的
    std::nth_element(vec.begin(), vec.begin() + 4, vec.end());

    // 输出排序后的结果
    std::cout << "第5个元素:" << vec[4] << std::endl;
    std::cout << "排序后的容器:" << std::endl;
    for (const auto& elem : vec) {
        std::cout << elem << " ";  //
    }
    std::cout << std::endl;

    return 0;
}

在这里插入图片描述

4.蓝桥杯例题

在这里插入图片描述

三、排序

1.sort函数

std::sort 是 C++ 标准库中的一个算法函数,用于对容器中的元素进行排序。它在 <algorithm> 头文件中声明。
first 和 last:表示要排序的范围的起始和结束迭代器(包含 first,但不包含 last)。
std::sort 函数对范围 [first, last) 中的元素执行排序操作。默认情况下,它使用 < 运算符来比较元素的大小。如果需要自定义排序规则,可以提供一个比较函数或者 lambda 函数作为额外参数。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {3, 1, 4, 1, 5, 9, 2, 6};

    // 对容器中的元素进行排序
    std::sort(vec.begin(), vec.end());

    // 输出排序后的结果
    std::cout << "排序后的容器:" << std::endl;
    for (const auto& elem : vec) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

2.sort自定义比较函数,或lambda表达式(匿名函数)

如果需要自定义排序规则,可以通过提供一个比较函数或者 lambda 函数来告诉 std::sort 函数如何进行排序。

#include <iostream>
#include <vector>
#include <algorithm>

// 自定义比较函数,按照元素的绝对值进行排序
bool compareAbsoluteValue(int a, int b) {
    return std::abs(a) < std::abs(b);
}

int main() {
    std::vector<int> vec = {3, -1, 4, -6, 5, 9, -2, 6};

    // 使用自定义比较函数对容器中的元素进行排序
    std::sort(vec.begin(), vec.end(), compareAbsoluteValue);
    /* lambda表达式(匿名函数)
    std::sort(vec.begin(), vec.end(), [](int a, int b) {
    return std::abs(a) < std::abs(b);
	});
	*/

    // 输出排序后的结果
    std::cout << "排序后的容器:" << std::endl;
    for (const auto& elem : vec) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

3.蓝桥杯例题

在这里插入图片描述

四、全排列

1.next_permutation函数

std::next_permutation 是 C++ 标准库中的一个算法函数,用于生成给定序列的下一个排列。它在 <algorithm> 头文件中声明。

bool next_permutation(BidirectionalIterator first, BidirectionalIterator last);
first 和 last:表示要处理的范围的起始和结束迭代器(包含 first,但不包含 last)。
std::next_permutation 函数会重新排列范围 [first, last) 中的元素,生成当前排列的下一个排列。如果存在下一个排列,则函数返回 true,否则返回 false。如果 first 和 last 之间的元素已经按字典序降序排列(即当前排列是最后一个排列),那么函数将重新排列这些元素为第一个排列,并返回 false。

#include <iostream>
#include <vector>
#include <algorithm> // 包含 next_permutation 函数

int main() {
    std::vector<int> vec = {1, 2, 3};

    // 打印初始排列
    std::cout << "初始排列: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 生成并打印下一个排列,直到没有下一个排列为止
    while (std::next_permutation(vec.begin(), vec.end())) {
        std::cout << "下一个排列: ";
        for (int num : vec) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

2. prev_permutation函数

std::prev_permutation 是 C++ 标准库中的一个算法函数,用于生成给定序列的上一个排列。它在 <algorithm> 头文件中声明。

bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
first 和 last:表示要处理的范围的起始和结束迭代器(包含 first,但不包含 last)。
std::prev_permutation 函数会重新排列范围 [first, last) 中的元素,生成当前排列的上一个排列。如果存在上一个排列,则函数返回 true,否则返回 false。如果 first 和 last 之间的元素已经按字典序升序排列(即当前排列是第一个排列),那么函数将重新排列这些元素为最后一个排列,并返回 false。

#include <iostream>
#include <vector>
#include <algorithm> // 包含 prev_permutation 函数

int main() {
    std::vector<int> vec = {3, 2, 1};

    // 打印初始排列
    std::cout << "初始排列: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 生成并打印上一个排列,直到没有上一个排列为止
    while (std::prev_permutation(vec.begin(), vec.end())) {
        std::cout << "上一个排列: ";
        for (int num : vec) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

五、大小写函数

1.islower,isupper函数

islower 和 isupper 是 C++ 标准库中的字符处理函数,用于检查字符是否为小写字母和大写字母。它在 <cctype> 头文件中声明。

int islower(int c);
c:表示要检查的字符,通常以整数形式传入。
islower 函数用于检查字符 c是否为小写字母。如果 c 是小写字母,则返回非零值(通常为 1),否则返回 0。

int isupper(int c);
c:表示要检查的字符,通常以整数形式传入。
isupper 函数用于检查字符 c是否为大写字母。如果 c 是大写字母,则返回非零值(通常为 1),否则返回 0。

#include <iostream>
#include <cctype> // 包含 islower 函数

int main() {
    char c = 'a';

    // 检查字符 c 是否为小写字母
    if (islower(c)) {
        std::cout << c << " 是小写字母" << std::endl;
    } else {
        std::cout << c << " 不是小写字母" << std::endl;
    }
    

    // 检查字符 c 是否为大写字母
    if (isupper(c)) {
        std::cout << c << " 是大写字母" << std::endl;
    } else {
        std::cout << c << " 不是大写字母" << std::endl;
    }

    return 0;
}

2.tolower,toupper函数

tolower 和 toupper 是 C++ 标准库中的字符处理函数,用于将字符转换为小写和大写形式。它在 <cctype> 头文件中声明。

int tolower(int c);
c:表示要转换的字符,通常以整数形式传入。
tolower 函数用于将字符 c 转换为小写形式。如果c 是大写字母,则返回相应的小写字母;否则,返回 c 自身。
int toupper(int c);
c:表示要转换的字符,通常以整数形式传入。
toupper 函数用于将字符 c 转换为大写形式。如果 c是小写字母,则返回相应的大写字母;否则,返回 c 自身。

#include <iostream>
#include <cctype> // 包含 tolower 函数

int main() {
    char c = 'A';
    char d = 'b';

    // 将字符 c 转换为小写形式
    char lowercase_c = tolower(c);

    std::cout << "字符 " << c << " 转换为小写形式为 " << lowercase_c << std::endl;


	// 将字符 d 转换为大写形式
    char uppercase_d = toupper(d);

    std::cout << "字符 " << d << " 转换为大写形式为 " << uppercase_d << std::endl;
    return 0;
}

六、其他库函数

1.memset 函数

memset 是 C/C++ 标准库中的一个函数,用于将一段内存区域的内容设置为指定的值。它在 <cstring> 头文件中声明。

memset 函数将指针 ptr 开始的连续 num 个字节的内存内容都设置为 value。
void* memset(void* ptr, int value, size_t num);
ptr:表示指向要填充的内存区域的指针。
value:表示要设置的值,以整数形式传入。
num:表示要设置的字节数。

#include <iostream>
#include <cstring> // 包含 memset 函数

int main() {
    char str[] = "Hello, world!";
    int size = sizeof(str);

    // 将 str 数组的内容全部设置为 'A'
    memset(str, 'A', size);

    std::cout << str << std::endl; // 输出 "AAAAAAAAAAAAAA"

    return 0;
}

2.swap 函数

std::swap 是 C++ 标准库中的一个函数模板,用于交换两个值。它在 <algorithm> 头文件中声明。
std::swap 函数用于交换 a 和 b 两个值。它可以用于任何数据类型,包括基本数据类型(如整数、浮点数)和自定义类型(如类对象)。

#include <iostream>
#include <algorithm> // 包含 swap 函数

int main() {
    int a = 10;
    int b = 20;

    // 交换 a 和 b 的值
    std::swap(a, b);

    std::cout << "a: " << a << std::endl; // 输出 "20"
    std::cout << "b: " << b << std::endl; // 输出 "10"

    return 0;
}

3.reverse 函数

std::reverse 是 C++ 标准库中的一个算法函数,用于反转容器中元素的顺序。它在 <algorithm> 头文件中声明。

void reverse(BidirectionalIterator first, BidirectionalIterator last);
first 和 last:表示要反转的范围的起始和结束迭代器(包含 first,但不包含 last)。
std::reverse 函数用于反转范围 [first, last) 中的元素的顺序。它逆序地重新排列容器中的元素,即将第一个元素与最后一个元素交换,第二个元素与倒数第二个元素交换,以此类推,直到中间位置。

#include <iostream>
#include <vector>
#include <algorithm> // 包含 reverse 函数

int main() {
    std::vector<int> vec = {1, 7, 3, 6, 5};

    // 反转容器中的元素顺序
    std::reverse(vec.begin(), vec.end());

    // 输出反转后的结果
    std::cout << "反转后的容器:" << std::endl;
    for (const auto& elem : vec) {
        std::cout << elem << " ";  //5 6 3 7 1
    }
    std::cout << std::endl;

    return 0;
}

4.unique 函数

std::unique 是 C++ 标准库中的一个算法函数,用于移除容器中相邻重复的元素,并返回指向新的逻辑结尾的迭代器。它在 <algorithm> 头文件中声明。

ForwardIterator unique(ForwardIterator first, ForwardIterator last);
first 和 last:表示要处理的范围的起始和结束迭代器(包含 first,但不包含 last)。
std::unique 函数用于移除范围 [first, last) 中相邻重复的元素,只保留每个不重复元素的第一个出现,并返回一个指向新的逻辑结尾的迭代器,指向不重复元素的下一个位置。

#include <iostream>
#include <vector>
#include <algorithm> // 包含 unique 函数

int main() {
    std::vector<int> vec = {1, 2, 2, 3, 6, 3, 3, 6, 6, 4, 5, 5};

    // 移除容器中相邻重复的元素
    auto new_end = std::unique(vec.begin(), vec.end());

    // 输出处理后的容器内容
    std::cout << "处理后的容器:" << std::endl;
    for (auto it = vec.begin(); it != new_end; ++it) {
        std::cout << *it << " "; //1 2 3 6 3 6 4 5
    }
    std::cout << std::endl;

    return 0;
}

总结

以上就是本章的介绍,唐怡佳继续加油!也希望大家都能取得好的成绩!

  • 26
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小唐YiJiaTang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值