【C++STL基础入门】排序和遍历容器

文章目录

  • 前言
  • 使用前须知
  • 一、for_each算法
    • [1.1 for_each是什么](#1.1 for_each是什么)
    • [1.2 函数原型](#1.2 函数原型)
    • [1.3 示例代码1:将容器中的每个元素打印出来](#1.3 示例代码1:将容器中的每个元素打印出来)
    • [1.4 示例代码2:将容器中的每个字符串转换为大写形式](#1.4 示例代码2:将容器中的每个字符串转换为大写形式)
  • 二、sort算法
  • 总结

前言

STL(Standard Template Library)是C++的标准库之一,提供了丰富的容器、迭代器和算法等组件,方便了C++开发者进行快速而高效的编程。其中,for_each和sort是STL中两个有用的小算法,用于对容器中的元素进行遍历和排序。本文将介绍这两个算法的概念、函数原型,并给出相应的示例代码,使用string类作为示例。


使用前须知

头文件

我们需要使用头文件#include <algorithm>

一、for_each算法

1.1 for_each是什么

for_each算法用于对容器中的元素逐个进行处理操作,它接受一个函数对象作为参数,并将容器中的每个元素传递给函数对象进行处理。

1.2 函数原型

cpp 复制代码
template<class InputIt, class UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f);

参数3的函数可以参数可以是容器的类型,也可以是容器

1.3 示例代码1:将容器中的每个元素打印出来

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>

void printElement(const std::string& elem) {
    std::cout << elem << " ";
}

int main() {
    std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"};

    std::for_each(names.begin(), names.end(), printElement);

    return 0;
}

示例代码1演示了如何使用for_each算法将容器names中的每个元素打印出来。首先定义了一个函数printElement,它将一个字符串作为参数并打印它。然后,在main函数中调用for_each算法,传入names.begin()和names.end()来指定元素范围,并将printElement作为函数对象进行处理。执行该代码,会输出:Alice Bob Charlie David,每个元素以空格分隔。

1.4 示例代码2:将容器中的每个字符串转换为大写形式

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>

void toUpper(std::string& elem) {
    std::transform(elem.begin(), elem.end(), elem.begin(), ::toupper);
}

int main() {
    std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"};

    std::for_each(names.begin(), names.end(), toUpper);

    for (const std::string& elem : names) {
        std::cout << elem << " ";
    }

    return 0;
}

示例代码2展示了如何使用for_each算法对容器names中的每个元素进行大写转换操作。我们定义了一个函数toUpper,它接受一个字符串的引用,并使用std::transform将字符串中的字符转换为大写形式。然后,在main函数中调用for_each算法,将names.begin()和names.end()指定的元素范围传递给算法,并将toUpper作为函数对象进行处理。最后,我们遍历names并打印结果:ALICE BOB CHARLIE DAVID。

二、sort算法

2.1 sort算法是什么?

sort算法用于对容器中的元素进行排序操作,可以按升序或降序排列。

2.2 函数原型

cpp 复制代码
template<class RandomIt>
void sort(RandomIt first, RandomIt last);

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

2.3 示例代码1:按升序排序容器中的字符串

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"};

    std::sort(names.begin(), names.end());

    for (const std::string& elem : names) {
        std::cout << elem << " ";
    }

    return 0;
}

示例代码1展示了如何使用sort算法按升序对容器names中的字符串进行排序。调用sort算法,传入names.begin()和names.end()指定的元素范围。执行该代码,会输出:Alice Bob Charlie David,即按照字母顺序排序的结果。

2.4示例代码4:按降序排序容器中的字符串

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>

bool compare(const std::string& a, const std::string& b) {
    return a > b;
}

int main() {
    std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"};

    std::sort(names.begin(), names.end(), compare);

    for (const std::string& elem : names) {
        std::cout << elem << " ";
    }

    return 0;
}

示例代码2展示了如何使用sort算法按降序对容器names中的字符串进行排序。我们定义了一个自定义比较函数compare,它接受两个字符串参数并比较它们的字典顺序。在调用sort算法时,传递了compare作为比较函数,以便在排序时按照降序进行排序。执行该代码,会输出:David Charlie Bob Alice,即按照字母逆序排序的结果。


总结

本文介绍了STL中的两个小算法:for_each和sort。for_each算法用于对容器中的元素进行遍历和处理操作,sort算法用于对容器中的元素进行排序。通过示例代码的演示,我们了解了这两个算法的概念、函数原型以及用法,并使用string类作为示例进行说明。这些算法在实际开发中很常用,能够极大地简化对容器的操作和排序需求。希望本文对你理解和使用STL算法有所帮助!

相关推荐
Eric.Lee20211 分钟前
python实现 mp4转gif文件
开发语言·python·手势识别·手势交互·手势建模·xr混合现实
EntyIU4 分钟前
python开发中虚拟环境配置
开发语言·python
乌萨奇也要立志学C++6 分钟前
【洛谷】递归初阶 三道经典递归算法题(汉诺塔 / 占卜 DIY/FBI 树)详解
数据结构·c++·算法
vyuvyucd25 分钟前
C++引用:高效编程的别名利器
算法
踏浪无痕32 分钟前
AI 时代架构师如何有效成长?
人工智能·后端·架构
程序员小假1 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端
鱼跃鹰飞1 小时前
Leetcode1891:割绳子
数据结构·算法
️停云️1 小时前
【滑动窗口与双指针】不定长滑动窗口
c++·算法·leetcode·剪枝·哈希
charlie1145141911 小时前
嵌入式现代C++教程: 构造函数优化:初始化列表 vs 成员赋值
开发语言·c++·笔记·学习·嵌入式·现代c++
wjs20241 小时前
Bootstrap5 消息弹窗
开发语言