13

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.

m.s.
  • 16,063
  • 7
  • 53
  • 88
rahul
  • 403
  • 3
  • 6
  • 15
  • 1
    Check out [Loki answer here](http://stackoverflow.com/questions/1430757/c-vector-to-string). He use `std::copy` to `cout` an `array`. Check it [running here.](https://ideone.com/DmR1SP) – wendelbsilva Oct 30 '15 at 15:08
  • 1
    One solution can be that you can overload cout operator '<<' – Bharadwaj Oct 30 '15 at 15:08

7 Answers7

13

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 2
    Yep, the second wins on all aspects :) (At least when C++11 is available) – Antonio Oct 30 '15 at 15:16
  • 2
    @Antonio: As it stands right now, the first also requires C++11 (C++03 doesn't have `std::begin` or `std::end`, though you can write analogs on your own). – Jerry Coffin Oct 30 '15 at 16:13
  • @JerryCoffin Yep, but I (personally) see the overhead implied by dropping C++11 more prominent in the second version. – Antonio Oct 30 '15 at 16:19
  • @Antonio: Fair enough. I do look forward to a standard library with Ranges--preferably ones with constructors that would let the first turn into something like: `std::copy(arra, {std::cout, "\n"});`. Now that I think about it, I'm tempted to see if 1) Eric Neibler's library supports that now, and 2) if it does, figure out if I can make it work). – Jerry Coffin Oct 30 '15 at 16:26
  • You don't want or need `std::endl` here; flushing the iostream buffer after every line will happen anyway if line buffered, and is very bad for performance if it wasn't. – Peter Cordes Apr 25 '20 at 00:39
3
#include<iostream>        
using namespace std;

int main(){ int i;

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

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

}

Revealer
  • 41
  • 1
  • 2
    Welcome to stackoverflow! Please make sure to read the question carefully before posting an answer, the OP clearly asked for a way to do it WITHOUT using a for loop. – Markoorn Apr 27 '19 at 08:31
1

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"));
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • There is a way to do this without loop. – SergeyA Oct 30 '15 at 15:09
  • Repeating that "10" is very dangerous (thinking about the first time the size of the array will change), I would rather go for something like `std::end(arra)` or at least defining the array size as a constant – Antonio Oct 30 '15 at 15:21
1

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::copy to fide the loop and display the array.

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

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

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.

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

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

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().

Vadiklk
  • 3,696
  • 5
  • 28
  • 44
-1

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] << " "; 
    } 
Antu
  • 2,197
  • 3
  • 25
  • 40