在 C++ 中计算整个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33439064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:16:54  来源:igfitidea点击:

Cout a whole array in c++

c++c++11visual-c++

提问by rahul

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?

我对 C++ 相当陌生,除了通过 for 循环进行迭代之外,在 C++ 中有没有一种方法可以通过它来计算整个静态数组?

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.

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

回答by Jarod42

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;
}

回答by m.s.

You need to either loop over the array

您需要循环遍历数组

int arra[10] = {1,2,3,4};
for (int i = 0; i<sizeof(arra)/sizeof(arra[0]); ++i)
{
    std::cout << arra[i] << std::endl;
}

or use

或使用

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

回答by NathanOliver

Some how you are going to have to visit each element of the array to display the contents. This can be done long form or use a loop. Fortunately we can use std::copyto fide the loop and display the array.

您将如何访问数组的每个元素以显示内容。这可以完成长格式或使用循环。幸运的是,我们可以使用std::copy来确定循环并显示数组。

int arr[] = {1,2,3,4,5};
std::copy(std::begin(arr), std::end(arr), std::ostream_iterator<int>(std::cout, " "));

Live Example

Live Example

回答by Vadiklk

As you asked to do this without an Array, you could easily do this:

当您要求在没有 Array 的情况下执行此操作时,您可以轻松地执行以下操作:

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

If you want to write good code, you could use std::array and then simply write arra.begin()and arra.end().

如果你想写出好的代码,你可以使用 std::array 然后简单地编写arra.begin()and arra.end()

回答by SergeyA

There are basically two ways. First one is a loop somewhere . The loop can be explicit - in your code - or it can be implicit through library. Example of library loop:

基本上有两种方式。第一个是某处的循环。循环可以是显式的 - 在您的代码中 - 也可以是通过库隐式的。库循环示例:

std::for_each(cbegin(arra), cend(arra), [](int i) {std::cout << "i ";});

The second way of printing the array is with the use of recursion. Here is example of the code:

打印数组的第二种方法是使用递归。以下是代码示例:

void print_element(const int* head, const int* tail) {
    if (head == tail)
       return;

    std::cout << *head << " ";
    print_element(head + 1, tail);
}

....
print_element(arr, arr + sizeof(arr) / sizeof(*arr));

Couple of words about recursion solution. Depending on your optimization, it can produce different results. Performance of the recursion will be roughly equivalent to the performance of the loop on any optimization level with AMD64 ABI. The reason for that is that arguments to functions are passed through registers (not pushed into stack), and the frame pointers are not used with any optimization. With this in mind, the only CALL/RET (which push/pop RIP) will slow down execution compared the loop, but this degradation is not measurable. The real issue, however, is that there is limited number of recursed calls which can be made on a given system (usually around single thousands), so printing an array of 1000000 elements is guaranteed to crash the application.

关于递归解决方案的几句话。根据您的优化,它可以产生不同的结果。递归的性能将大致相当于使用 AMD64 ABI 的任何优化级别上的循环性能。原因是函数的参数通过寄存器传递(而不是压入堆栈),并且帧指针不用于任何优化。考虑到这一点,与循环相比,唯一的 CALL/RET(推/弹出 RIP)会减慢执行速度,但这种降级是不可测量的。然而,真正的问题是在给定系统上可以进行的递归调用数量有限(通常大约为数千个),因此打印 1000000 个元素的数组肯定会导致应用程序崩溃。

With higher levels of optimization which involve tail-call optimization, the recursion calls will be translated into plain jmps and the performance will be exactly the same as one with the loop. It will also eliminate the problem of maximum recursion level - so that arrays of any sizes can be printed.

随着涉及尾调用优化的更高级别的优化,递归调用将被转换为普通的 jmps,性能将与循环完全相同。它还将消除最大递归级别的问题 - 以便可以打印任何大小的数组。

回答by Naveen Lakhchaura

It is printing address because you are pointing to an array, not its elements. try this-

它正在打印地址,因为您指向的是一个数组,而不是它的元素。尝试这个-

void printArray(int arr[], int n) 

/* n is size here*/

{ 
    for (int i = 0; i < n; i++) 

        cout << arr[i] << " "; 
    } 

回答by Revealer

#include<iostream>        
using namespace std;

int main(){ int i;

int main(){ int i;

int myarr[5]={9,84,7,55,6};
for(i=0;i<5;i++){

    cout<<myarr[i]<<endl;
}

}

}