在 C++ 中打印数组?
有没有办法在 C++ 中打印数组?
我正在尝试创建一个函数来反转用户输入数组,然后将其打印出来。我尝试用谷歌搜索这个问题,似乎 C++ 无法打印数组。这不可能是真的吧?
Is there a way of printing arrays in C++?
I'm trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and it seemed like C++ can't print arrays. That can't be true can it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
只需迭代元素即可。像这样:
注意:正如 Maxim Egorushkin 指出的,这可能会溢出。请参阅下面他的评论以获得更好的解决方案。
Just iterate over the elements. Like this:
Note: As Maxim Egorushkin pointed out, this could overflow. See his comment below for a better solution.
使用STL
Use the STL
我可以建议使用鱼骨运算符吗?
(你能发现吗?)
May I suggest using the fish bone operator?
(Can you spot it?)
除了基于 for 循环的解决方案之外,您还可以使用 ostream_iterator。下面是一个利用(现已停用的)SGI STL 参考中的示例代码的示例:
这会生成以下内容:
但是,这对于您的需求来说可能有点过分了。一个直接的 for 循环可能就是您所需要的,尽管 litb 的模板糖也相当不错。
编辑:忘记了“反向打印”要求。这是一种实现方法:
输出:
编辑:C++14 更新,使用 std::begin() 和 std::rbegin():
Besides the for-loop based solutions, you can also use an ostream_iterator<>. Here's an example that leverages the sample code in the (now retired) SGI STL reference:
This generates the following:
However, this may be overkill for your needs. A straight for-loop is probably all that you need, although litb's template sugar is quite nice, too.
Edit: Forgot the "printing in reverse" requirement. Here's one way to do it:
and the output:
Edit: C++14 update that simplifies the above code snippets using array iterator functions like std::begin() and std::rbegin():
有已声明的数组和未声明但以其他方式创建的数组,特别是使用
new
:具有 3 个元素的数组是动态创建的(并且
3
也可以在运行时计算),并且将一个指向它的指针(其大小已从其类型中删除)分配给p
。您无法再获取打印该数组的大小。因此,仅接收指向它的指针的函数无法打印该数组。打印声明的数组很容易。您可以使用
sizeof
获取它们的大小,并将该大小传递给函数,包括指向该数组元素的指针。但是您也可以创建一个接受数组的模板,并从其声明的类型推断其大小:这样做的问题是它不接受指针(显然)。我认为最简单的解决方案是使用 std::vector 。它是一个动态的、可调整大小的“数组”(具有您期望的真实数组的语义),它有一个
size
成员函数:当然,您也可以将其作为接受向量的模板其他类型的。
There are declared arrays and arrays that are not declared, but otherwise created, particularly using
new
:That array with 3 elements is created dynamically (and that
3
could have been calculated at runtime, too), and a pointer to it which has the size erased from its type is assigned top
. You cannot get the size anymore to print that array. A function that only receives the pointer to it can thus not print that array.Printing declared arrays is easy. You can use
sizeof
to get their size and pass that size along to the function including a pointer to that array's elements. But you can also create a template that accepts the array, and deduces its size from its declared type:The problem with this is that it won't accept pointers (obviously). The easiest solution, I think, is to use
std::vector
. It is a dynamic, resizable "array" (with the semantics you would expect from a real one), which has asize
member function:You can, of course, also make this a template to accept vectors of other types.
C++ 中常用的大多数库本身都不能打印数组。您必须手动循环它并打印出每个值。
打印数组和转储许多不同类型的对象是高级语言的一个功能。
Most of the libraries commonly used in C++ can't print arrays, per se. You'll have to loop through it manually and print out each value.
Printing arrays and dumping many different kinds of objects is a feature of higher level languages.
确实是!您必须循环遍历数组并单独打印每个项目。
It certainly is! You'll have to loop through the array and print out each item individually.
这可能有帮助
//打印数组
n 是数组的大小
This might help
//Printing The Array
n is the size of the array
工作原理基本上与 foreach 类似。
Works basically like foreach.
我的简单回答是:
My simple answer is:
您可以使用反向迭代器反向打印数组:
输出
如果您已经反转了数组,则可以分别将
std::rbegin
和std::rend
替换为std::begin
/std::end
,向前迭代数组。You can use reverse iterators to print an array in reverse:
output
If you already reversed the array, you can replace
std::rbegin
andstd::rend
withstd::begin
/std::end
, respectively, to iterate the array in forward direction.将数组的元素复制到合适的输出迭代器非常简单。例如(使用 C++20 作为 Ranges 版本):
快速演示:
当然,如果我们可以输出任何类型的集合,而不仅仅是数组,那就更有用了:
问题提到了反转数组以进行打印。通过使用视图适配器可以轻松实现这一点:
It's quite straightforward to copy the array's elements to a suitable output iterator. For example (using C++20 for the Ranges version):
Quick demo:
Of course it's more useful if we can output any kind of collection, not only arrays:
The question mentions reversing the array for printing. That's easily achieved by using a view adapter:
如果你想创建一个打印数组中每个元素的函数;
上面的函数仅打印数组中的每个元素,而不是逗号等。
您可能想知道“为什么 sizeoff(arr) 除以 4?”。这是因为如果数组中只有一个元素,cpp 会打印 4。
If you want to make a function that prints every single element in an array;
The function above prints every single element in an array only, not commas etc.
You may be wondering "Why sizeoff(arr) divided by 4?". It's because cpp prints 4 if there's only a single element in an array.