delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/23/14:15:57

Newsgroups: comp.os.msdos.djgpp
Subject: Re: newbie: pointers/vectors confusion w/ DJGPP
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
References: <87752c88 DOT 0307230158 DOT 15540458 AT posting DOT google DOT com>
Organization: Nocturnal Aviation
Message-ID: <Xns93C18634DF202mambuhlearthlinknet@207.217.77.25>
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<c=AN_wKI(G1PX/L02Q~BX<O}T<@916W22JYTnlp`C:a~[$WAdF9XpX'x!3jFgPK&*O6'^%RkX-C`F$7A7^}V$.K=
Lines: 67
Date: Wed, 23 Jul 2003 18:11:58 GMT
NNTP-Posting-Host: 65.148.4.49
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread2.prod.itd.earthlink.net 1058983918 65.148.4.49 (Wed, 23 Jul 2003 11:11:58 PDT)
NNTP-Posting-Date: Wed, 23 Jul 2003 11:11:58 PDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On 23 Jul 2003, you wrote in comp.os.msdos.djgpp:

> 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 <iostream>
> #include <vector>
> 
> using namespace std;
> 
> int main() {
> 
>      vector<int> *demand; 
> 
>      for (int i=0; i<10; ++i) 
>      {
>           demand->push_back(7);
>      }
> 
>      for (vector<int>::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 <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> demand;
    for (int i = 0; i < 10; ++i)
        demand.push_back(7);
    for (vector<int>::iterator it = demand.begin();
         it != demand.end(); ++it)
        cout << *it << "\n";
}



-- 
Martin Ambuhl
Returning soon to the
Fourth Largest City in America

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019