在 C++ 中打印数组? [英] Printing an array in C++?

查看:31
本文介绍了在 C++ 中打印数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 C++ 中打印数组?

Is there a way of printing arrays in C++?

我正在尝试创建一个函数来反转用户输入数组,然后将其打印出来.我试过谷歌搜索这个问题,似乎 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?

推荐答案

只需遍历元素即可.像这样:

Just iterate over the elements. Like this:

for (int i = numElements - 1; i >= 0; i--) 
    cout << array[i];

注意:正如 Maxim Egorushkin 所指出的,这可能会溢出.请参阅下面的评论以获得更好的解决方案.

Note: As Maxim Egorushkin pointed out, this could overflow. See his comment below for a better solution.

这篇关于在 C++ 中打印数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆