Thread: help with vector..

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    help with vector..

    Code:
    #include <iostream>
    #include <vector>
    #include <windows.h>
    
    using namespace std;
    using std::vector;
    int main()
    {
          vector<int> vi;
          for (int i = 0; i < 100; i++)
          {
          Sleep(5000);
          vi.push_back(i);
          
          cout< <<endl; //heres the prob , i want to cout vi[0] then vi[1]
          Sleep(5000);      //as the for loop assigns values to the vector 
          }
    return 0;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    So you want this?
    Code:
    std::cout<<vi[i]<<std::endl;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i want to cout vi[0] then vi[1]

    What is the problem? Other than the first time through the loop when vi[1] is invalid, you can do that if you want. It doesn't seem to make a lot of sense, though, maybe you could describe better what you are trying to do.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    ?
    Code:
    #include <iostream>
    #include <vector>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
    	vector<int> vi;
    	for (int i = 0; i < 100; i++)
    	{
    	//	Sleep(5000);
    		vi.push_back(i);
    	}
    	cout << vi[0] << vi[1];
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cout< <<endl;
    Three < ?
    Where's the variable you want to output?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    thanks for the quick replys ...
    i was doing vi[0][i] so it would start from 0 element
    the whole idea was i didnt know how to cout in order as the for loop assigns
    Last edited by Anddos; 03-18-2006 at 01:36 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You could also use vi.at(i) instead of vi[i]

    The added bonus with at() is that it will range-check for you, in case somewhere along the line your program doesn't manage to fill the element you are trying to retrieve. (it will throw an error - But that is far better than the undefined behaviour you would otherwise get)

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