GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Mar-2005, 14:36
iamsodumb iamsodumb is offline
New Member
 
Join Date: Mar 2005
Location: Mumbai
Posts: 27
iamsodumb is on a distinguished road

problem with vectors


see this

CPP / C++ / C Code:

vector<int> :: iterator it;
int i;
vector<int> A;
for (i=0;  i<5;  i++){
       A.push_back(i);
}

for(it = A.begin(); it != A.end(); it++){
cout<<*it;
}


Now the output is 0123 coz the loop is stoping when it = A.end()

can anyone please tell me how I can get 01234
  #2  
Old 18-Mar-2005, 15:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by iamsodumb
s

Now the output is 0123 coz the loop is stoping when it = A.end()

can anyone please tell me how I can get 01234

There's nothing obviously wrong with your code. (When the iterator is equal to A.end(), it is past the last item, so the logic is OK.)

The only thing I can think of is that you don't flush the output buffer. Sometimes it makes a difference, depending on what else your program does.

Here is a complete program; run it and tell me if you see any difference:

CPP / C++ / C Code:
#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

int main()
{

  vector<int> :: iterator it;
  int i;
  vector<int> A;
  for (i = 0; i < 5; i++){
    A.push_back(i);
  }

  cout << "A.size() = " << A.size() << endl;

  for(it = A.begin(); it != A.end(); it++){
    cout<< *it;
  }

  cout << endl;

  return 0;
}

My output:

Quote:
A.size() = 5
01234

Regards,

Dave
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help! Insert into database problem robsmith MySQL / PHP Forum 1 24-Apr-2005 03:44
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 13:51
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
Declaring a vector of vectors? Lethal411 C++ Forum 2 20-Mar-2004 09:02
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.