在 C++ 中打印数组的内容

转自:https://www.techiedelight.com/zh/print-contents-array-cpp

在 C++ 中打印数组的内容
这篇文章将讨论如何在 C++ 中打印数组的内容。

1. 使用数组索引

一个简单的解决方案是遍历数组的元素并打印每个元素。

#include <iostream>

// 在 C++ 中使用数组索引打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };
    size_t n = sizeof(input)/sizeof(input[0]);

    //循环遍历数组元素
    for (size_t i = 0; i < n; i++) {
        std::cout << input[i] << ' ';
    }

    return 0;
}

输出:

1 2 3 4 5

上面的代码使用 sizeof 用于确定数组大小的运算符。我们还可以创建一个模板,从其声明的类型中推断出数组的大小。

#include <iostream>

template<typename T, size_t n>
void print_array(T const(& arr)[n])
{
    for (size_t i = 0; i < n; i++) {
        std::cout << arr[i] << ' ';
    }
}

// 在 C++ 中使用模板打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };

    print_array(input);

    return 0;
}

输出:

1 2 3 4 5

2. 使用 std::copy 和 std::ostream_iterator 功能

我们可以使用 Ostream 迭代器来写入输出流。这个想法是使用输出迭代器 std::ostream_iterator 将数组内容打印到 std::cout 在…的帮助下 std::copy,它将输入迭代器带到数组的开始和结束位置以及输出迭代器。

#include <iostream>
#include <algorithm>
#include <iterator>

// 在 C++ 中使用 `std::ostream_iterator` 打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(input)/sizeof(input[0]);

    std::copy(input, input + n,
            std::ostream_iterator<int>(std::cout, " "));

    // 或者,在 C++11 中使用 `std::begin` 和 `std::end`
    // std::copy(std::begin(input), std::end(input),
    //        std::ostream_iterator<int>(std::cout, " "));

    return 0;
}

输出:

1 2 3 4 5

使用 C++17,我们可以使用 std::copy 和 std::experimental::ostream_joiner 在标题中定义 <experimental/iterator>.它是一个单遍输出迭代器,可以将连续的数组元素写入 std::cout, 使用 << 运算符,每两个元素之间用分隔符分隔。

#include <iostream>
#include <experimental/iterator>

int main()
{
    int input[] = { 1, 2, 3, 4, 5 };

    std::copy(std::begin(input), std::end(input),
            std::experimental::make_ostream_joiner(std::cout, " "));

    return 0;
}

输出:

1 2 3 4 5

3. 使用基于范围的for循环

从 C++11 开始,推荐的方法是使用基于范围的 for 循环,如下所示:

#include <iostream>

// 在 C++ 中使用基于范围的 for 循环打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };

    for (const auto &value: input) {
        std::cout << value << ' ';
    }

    return 0;
}

输出:

1 2 3 4 5

4. 使用迭代器

即使数组不是 STL 容器,我们甚至可以使用迭代器来打印数组内容。这个想法是使用 std::begin 和 std::end 在 C++11 中引入。 std::start 将迭代器返回到开头并且 std::end 返回一个迭代器到给定数组末尾之后的位置。由于我们没有修改任何东西,我们应该使用 std::cbegin 和 std::cend,它返回常量迭代器。

#include <iostream>

// 在 C++ 中使用迭代器打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };

    for (auto it = std::cbegin(input); it != std::cend(input); it++) {
        std::cout << *it << ' ';
    }

    return 0;
}

输出:

1 2 3 4 5

5. 使用 std::for_each 功能

另一个不错的选择是使用 std::for_each 算法,它将指定的函数应用于由两个迭代器定义的指定范围内的每个元素。该函数也可以是重载类的对象 ()operator 或 lambda 表达式。

功能

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

void print(const int &i) {
    std::cout << i << ' ';
}

// 在 C++ 中使用 `std::for_each` 打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };

    std::for_each(std::begin(input), std::end(input), print);

    return 0;
}

class

#include <iostream>
#include <vector>
#include <algorithm>
 
struct myclass
{
    void operator() (int i) {
        std::cout << i << ' ';
    }
} ob;
 
// 在 C++ 中使用 `std::for_each` 打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };
 
    std::for_each(std::begin(input), std::end(input), ob);
 
    return 0;
}

lambda

#include <iostream>
#include <vector>
#include <algorithm>
 
// 在 C++ 中使用 `std::for_each` 打印数组的内容
int main()
{
    int input[] = { 1, 2, 3, 4, 5 };
 
    std::for_each(std::begin(input),
                std::end(input),
                [](const int &e) {
                    std::cout << e << " ";
                });
 
    return 0;
}

输出:

1 2 3 4 5

这就是在 C++ 中打印数组的内容。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值