迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中循环遍历数组

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将说明如何使用 C++ 中的不同方法遍历数组。

使用 for 循环在数组上迭代的方法

对数组元素进行遍历的一个明显可见的方法是 for 循环。它由三部分语句组成,每一部分用逗号分隔。首先,我们应该初始化计数器变量-i,根据设计,它只执行一次。接下来的部分声明一个条件,这个条件将在每次迭代时被评估,如果返回 false,循环就停止运行。在这种情况下,我们比较计数器和数组的大小。最后一部分递增计数器,它也在每个循环周期执行。

#include <iostream>
#include <array>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::array;

int main() {
    array<string, 10> str_arr = {"Albatross", "Auklet",
                              "Bluebird", "Blackbird",
                              "Cowbird", "Dove",
                              "Duck", "Godwit",
                              "Gull", "Hawk"};

    for (size_t i = 0; i < str_arr.size(); ++i) {
        cout << str_arr[i] << " - ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

Albatross - Auklet - Bluebird - Blackbird - Cowbird - Dove - Duck - Godwit - Gull - Hawk -

使用基于范围的循环来迭代一个数组

基于范围的循环是传统 for 循环的可读版本。这种方法是一种强大的替代方法,因为它在复杂的容器上提供了简单的迭代,并且仍然保持了访问每个元素的灵活性。下面的代码示例是前面例子的精确再实现。

#include <iostream>
#include <array>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::array;

int main() {
    array<string, 10> str_arr = {"Albatross", "Auklet",
                              "Bluebird", "Blackbird",
                              "Cowbird", "Dove",
                              "Duck", "Godwit",
                              "Gull", "Hawk"};

    for (auto &item : str_arr) {
        cout << item << " - ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

使用 std::for_each 算法遍历数组

for_each 是一个强大的 STL 算法,用于操作范围元素和应用自定义定义的函数。它将范围起始和最后一个迭代器对象作为前两个参数,函数对象作为第三个参数。本例中,我们将函数对象声明为 lambda 表达式,直接将计算结果输出到控制台。最后,我们可以将 custom_func 变量作为参数传递给 for_each 方法,对数组元素进行操作。

#include <iostream>
#include <array>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::array; using std::for_each;

int main() {
    array<int, 10> int_arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    auto custom_func = [](auto &i) { cout << i*(i+i) << "; "; };;

    for_each(int_arr.begin(), int_arr.end(), custom_func);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

2; 8; 18; 32; 50; 72; 98; 128; 162; 200;

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便