赞
踩
转自:https://www.techiedelight.com/zh/print-contents-array-cpp
在 C++ 中打印数组的内容
这篇文章将讨论如何在 C++ 中打印数组的内容。
一个简单的解决方案是遍历数组的元素并打印每个元素。
#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
我们可以使用 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
从 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
即使数组不是 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
另一个不错的选择是使用 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++ 中打印数组的内容。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。