在C ++中遍历整个数组 [英] Cout a whole array in c++

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

问题描述

我对C ++还是很陌生,除了可以通过for循环进行迭代之外,c ++中还有什么方法可以让我们排除整个静态数组?

I am fairly new to c++, is there a way in c++ through which we can cout a whole static array apart from iterating via a for loop?

int arra[10] = {1,2,3,4};
std::cout << arra << std::endl;

我试过了,但这是打印数组中第一个元素的地址。

I tried this but, this is printing address of the first element in the array.

推荐答案

以下操作不(明确地)使用循环:

Following doesn't use (explicitly) loop:

std::copy(std::begin(arra),
          std::end(arra),
          std::ostream_iterator<int>(std::cout, "\n"));

但循环似乎更易于读取/写入/理解:

but loop seems simpler to read/write/understand:

for (const auto& e : arra) {
    std::cout << e << std::endl;
}

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

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