Newsgroups: comp.os.msdos.djgpp Subject: Re: newbie: pointers/vectors confusion w/ DJGPP From: Martin Ambuhl References: <87752c88 DOT 0307230158 DOT 15540458 AT posting DOT google DOT com> Organization: Nocturnal Aviation Message-ID: User-Agent: Xnews/5.04.25 X-Face: #PT?r&aN>ro*?:r AT h&l1(3WchlNz?9-y/wnr]CjPxdjd/*4lVC\*M+aZLO?E!2Q)]vD*M8oL(Zu2:VR17$t7-eVdNQH'!xp'5'NAP4$\fb"zAcf60HTC%S!<~xD}TFE76>a)IF-xa%$2at+D^;7~$mW^aDR)]bBmO``s Hi > > I have neither experience with DJGPP nor (much) with C++ > in general but I wanted to run a small test program to > test the speed of execution in C/C++ with another application > under Windows. > > However, I seem to have fallen at the first hurdle.. > > It is necessary for the task in question to use containers and > I had wanted to use vectors. However I cannot seem to interpret > the behaviour of the following small program: > > #include > #include > > using namespace std; > > int main() { > > vector *demand; > > for (int i=0; i<10; ++i) > { > demand->push_back(7); > } > > for (vector::iterator it=demand->begin(); > it!=demand->end(); > ++it) > { > cout << *it << "\n"; > } > > return 0; > } > > I had hoped it would print 10 7s on the screen. Instead it sends > about 800 lines of ints follow by the 10 7s. You don't want a pointer to a vector, and if you did, you should have initialized that pointer to point at something. The following does what you want: #include #include using namespace std; int main() { vector demand; for (int i = 0; i < 10; ++i) demand.push_back(7); for (vector::iterator it = demand.begin(); it != demand.end(); ++it) cout << *it << "\n"; } -- Martin Ambuhl Returning soon to the Fourth Largest City in America