Thread: displaying a vector

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Question displaying a vector

    Does any one know how to display a vector after inputing numbers into the vector? Help

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Do you mean STL vector? If so you can access the vector the same way as an array, using the subscript operators -

    vector <int> yourVector(300);

    yourVector[0]=10;

    cout << yourVector[0];

    //etc

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    20
    if it's a vector like a resizeable array, here's what you do.

    Code:
    for (int i(0); i < yourVector.size(); i++){
         cout<<yourVector[i]<<"\n";
    }
    This will display each value in your vector on a separate line, assuming ostream<< has been defined for whatever you're storing in your vector. yourVector.size() may vary from vector class to vector class. it's size() in mine, but others use length() or numElements().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM