Why does vector.end() point beyond last element?

From: as mellow as a horse (mail_at_MICKmoss42.fslife.co.uk)
Date: 06/27/04


Date: Sat, 26 Jun 2004 23:59:02 +0100

You'll probably guess I'm only just learning this stuff so this might be a
stupid question. Given:

vector<int> b1;
vector<int>::iterator b1_iter;

b1.push_back(1); b1.push_back(2); b1.push_back(3); b1.push_back(4);

b1_iter = b1.begin();

cout << "b1_iter= " << *b1_iter << endl; // shows "1", fine

cout << "b1_iter= " << ++(*b1_iter) << endl; // shows "2", fine

b1_iter = b1.end();

cout << "b1_iter= " << *b1_iter << endl; // shows rubbish!

cout << "b1_iter= " << *(--b1_iter) << endl; // shows "4" as it should do

Why does b1.end() point 1 memory location past the 4th element? I was
getting rubbish until I had a brainwave and tried some fiddly pointer
arithmetic. Am I missing something?



Relevant Pages

  • Re: Attn: Gary Carson
    ... You asked a really stupid question and I just wasn't going to even try ... Learning how to ask the right question is an important tool in life. ... > copy, distribute, or retain this message without my authority. ... My guess would be that selling a manuscript to the kind of nutcase who ...
    (rec.gambling.poker)
  • Re: Mnemonic
    ... Santayana anyone? ... filling life with too much rubbish that ... We can safely assume you have no interest in learning from past ...
    (comp.lang.c)
  • Re: accident help
    ... Rubbish. ... It is called learning from experience. ... It is better to be prepared than caught unawares. ... Multitude of things for beer, cycling, Morris and curiosities at ...
    (uk.rec.cycling)
  • Re: Speeding fine couple seek review
    ... hell is a SPECS? ... It's not a question of learning to use one, ... spend hours looking at hundreds of pages of unrelated rubbish. ...
    (uk.rec.driving)
  • Re: Why does vector.end() point beyond last element?
    ... as mellow as a horse wrote: ... > You'll probably guess I'm only just learning this stuff so this might be a ... > stupid question. ...
    (alt.comp.lang.learn.c-cpp)