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
    Code:
    cin >> anArray;
    cout << anArray;
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

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

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

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



    -Prelude
    My best code is written with the delete key.

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

    Others:
    MinGW on XP

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

    search result list from google.com
    http://www.google.com/search?hl=en&i...tor.h+tutorial

    best search result I could find from the first page results of the search
    http://www.cs.brown.edu/people/jak/p...tltut/tut.html
    think only with code.
    write only with source.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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.

    http://www.cprogramming.com/tutorial/stl/vector.html
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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.)

    -Skipper
    "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