On 2007-07-19 17:56,
wrote:
> I have declared a vector like this:
> typedef vector <int> myvec;
>
> 1. myvec vec1;
> 2. vec1.push_back(4);
> 3. vec1[100] = 5;
>
> The question is how come line 3 works (no crash). But when I look at
> the size, the size is 1.
Because you are lucky. Trying to access an element that does not exist
is undefined behaviour and while things might look like they work you
can get yourself into deep trouble with this kind of code (since you
could be fiddling with memory belonging to something else). If unsure if
the element exist or not use vec1.at(100) instead.
--
Erik Wikström