views:

525

answers:

7

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?

+1  A: 

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.

Faxwell Mingleton
You can do it one statement in C++. I don't see this as a missing feature as using iterators makes it so much more powerful. Now I can abstract away where I am printing: it does not matter if the destination is console, pipe, file, another container, a function, a DB. It all looks and behaves the same.
Martin York
+2  A: 

It certainly is! You'll have to loop through the array and print out each item individually.

Andy Mikula
standard algorithms dont require a loop. (though thay may use one internally).
Martin York
What if I don't have access to the STL?
Andy Mikula
Then you aren't using C++. Perhaps you're using Symbian/C++, or some subset of C++ provided by an embedded compiler. (Granted that technically, the STL is a precursor to the standard libraries in standard C++. But usually when people say "STL", they mean "the class and function templates in the C++ standard libraries", not the pre-standard STL. Hence STL is a subset of C++).
Steve Jessop
+6  A: 

can't you just iterate over the elements? like this:

for (int i = numElements - 1; i >= 0; i--) 
    cout << array[i];
Botz3000
I'd also like to note, that in C++ world you mostly don't create a new, reversed array and then print it. Instead, you usually iterate the array in the other way: for (i = numElements-1; i>=0; i--) cout << array[i] << ",";
Pavel Shved
You forgot to declare the i as an integer didn' you? That's what I did (well not really since I was supposed to reverse it).. so doing something like "for (int i = staerd-1; i >= 0; i--)" is the best way?
Oh. Thanks for pointing that out. My brain is already shutting down i guess.
Botz3000
+1  A: 

C++ can print whatever you want if you program it to do so. You'll have to go through the array yourself printing each element.

Cogwheel - Matthew Orlando
+3  A: 

There are declared arrays and arrays that are not declared, but otherwise created, particularly using new:

int *p = new int[3];

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 to p. 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:

template<typename Type, int Size>
void print(Type const(& array)[Size]) {
  for(int i=0; i<Size; i++)
    std::cout << array[Size] << std::endl;
}

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 a size member function:

void print(std::vector<int> const &v) {
  std::vector<int>::size_type i;
  for(i = 0; i<v.size(); i++)
    std::cout << v[i] << std::endl;
}

You can, of course, also make this a template to accept vectors of other types.

Johannes Schaub - litb
What, no std::copy_n to an ostream_iterator? ;-)
Steve Jessop
+2  A: 

Besides the for-loop based solutions, you can also use an ostream_iterator<>. Here's an example that leverages the sample code in the SGI STL reference:

#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
  short foo[] = { 1, 3, 5, 7 };

  using namespace std;
  copy(foo,
       foo + sizeof(foo) / sizeof(foo[0]),
       ostream_iterator<short>(cout, "\n"));
}

This generates the following:

 ./a.out 
1
3
5
7

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:

#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
  short foo[] = { 1, 3, 5, 7 };

  using namespace std;

  reverse_iterator<short *> begin(foo + sizeof(foo) / sizeof(foo[0]));
  reverse_iterator<short *> end(foo);

  copy(begin,
       end,
       ostream_iterator<short>(cout, "\n"));
}

and the output:

$ ./a.out 
7
5
3
1
Void
+8  A: 

Use the STL

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::vector<int>    userInput;

    // Read until end of input.
    // Hit control D  
    std::copy(std::istream_iterator<int>(std::cin),
              std::istream_iterator<int>(),
              std::back_inserter(userInput)
             );

    // Print in Normal order
    std::copy(userInput.begin(),
              userInput.end(),
              std::ostream_iterator<int>(std::cout,",")
             );
    std::cout << "\n";

    // Print in reverse order:
    std::copy(userInput.rbegin(),
              userInput.rend(),
              std::ostream_iterator<int>(std::cout,",")
             );
    std::cout << "\n";
}
Martin York
Nicely done, Martin.
Void