Thread: Can you cout<< an array?

  1. #1

    Can you cout<< an array?

    Is it possible to use cout<< on an array? if not (or if it is easier) how else could i print the entire contents of an array on one line? like
    cin >> anArray;
    cout << anArray;
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    MinGW on XP

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    for( int x = 0; x < ARRAYCOUNT; x++ )
        cout << array[x] << endl;
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Arrays are no fun, use a vector and do this:

    copy ( array.begin(), array.end(), ostream_iterator</* Vector type */> ( cout ) );

    My best code is written with the delete key.

  4. #4
    i am clueless on vectors. Know any good tutorials?
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    MinGW on XP

  5. #5
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    vector.h, I believe, is a class that makes your life easier when using vectors,strings,and such.

    search result list from

    best search result I could find from the first page results of the search
    think only with code.
    write only with source.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    I think that tutorial is very old. The standard says vector (and the whole standard library for that matter), and are in namespace std.

    The tutorial hosted by this very site is at least standard in it's conventions.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Apr 2002
    Any reference containing information about the STL (Standard Template Library) will get you squared away on "containers", "algorithms" and "iterators".

    A standard array is, in fact, a container. A vector is an array (container) with "smart" memory management going for it.

    Where a typical array is predefined in size, a vector can expand/collapse as data is added/deleted. (Why didn't you find out about this earlier, right? Hence, Prelude's suggestion.)

    Her example code, in a nutshell, is to start with the first element (begin()) of the vector (array) and 'cout' each element up to, and including, the last element (end()).

    Oh, what about 'ostream_iterator</* Vector type */>'? The iterator, here, is 'cout' (the standard ouput iterator) and the Vector type is whatever data-type you declare, i.e. int, char, string, etc.

    Last thing. What's with "copy"? Well, we don't have to literally copy our vector to another 'container'. In this case, we send our "copy" to the standard output device, the screen.

    Note: toaster and SilentStrike have included a couple of links. Cool.
    Originally posted by Prelude
    Arrays are no fun,...
    Okay, pick a specific subject area, where we should all know what's going on, that generates much more confusion than standard arrays. How can this not be "fun"? (I'm speaking rhetorically, so, please, take it as such.)

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM