当前位置:   article > 正文

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

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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出:

1 2 3 4 5
  • 1

上面的代码使用 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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

输出:

1 2 3 4 5
  • 1

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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

输出:

1 2 3 4 5
  • 1

使用 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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出:

1 2 3 4 5
  • 1

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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出:

1 2 3 4 5
  • 1

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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出:

1 2 3 4 5
  • 1

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

输出:

1 2 3 4 5
  • 1

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/208843
推荐阅读
相关标签
  

闽ICP备14008679号