From -743379678526509633
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,6d7908504577ecbe
X-Google-Attributes: gidf78e5,public
From: seitz@ix.netcom.com (Matt Seitz)
Subject: Re: Why this behavior?
Date: 1997/09/19
Message-ID: <5vsrmv$7u0@kane.ico.net>#1/1
X-Deja-AN: 273940715
References: <Pine.PMDF.3.95.970913102712.587315117A-100000@WCUVAX1.WCU.EDU>
X-Original-Date: Fri, 19 Sep 1997 03:29:53 GMT
Originator: austern@isolde.mti.sgi.com
Organization: ICOnetworks, Scotts Valley, CA. 408-461-4638
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBNCL/qUy4NqrwXLNJAQECAwIAqrVTH2qYvfRZxjXXK3df1iOy6AoAQxUS F0+DqbAbMPqQwJQm4cMqmqyqBdqIPJhtH25KgT0ueZsZnd0BbIkl7w== =nOLL
Newsgroups: comp.std.c++


In article <Pine.PMDF.3.95.970913102712.587315117A-100000@WCUVAX1.WCU.EDU>, Carl Mummert <cm11214@WCUVAX1.WCU.EDU> wrote:
>I am curious about a behavior fo the vector class.
>
>What is the reasoning behind this not working:
>
>vector<int> vec(3);
>vec[5] = 2;  //not correct; operator[] won't
>             //allocate space

According to Stroustrup. the reason was efficiency (C++ PROGRAMMING LANGUAGE, 
3rd Ed., section 16.3.3).  In order for vec[n] to automatically allocate 
space, it would need to compare n to the current vector size every time vec[n] 
is used.  In order to better match the speed of C arrays, it was decided that 
vectors would not do range checking when the subscript operator was used.


 
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



