Why does vector.end() point beyond last element?
From: as mellow as a horse (mail_at_MICKmoss42.fslife.co.uk)
Date: 06/27/04
- Next message: Bob R: "Re: C++ and graphics. [OT]"
- Previous message: Mark : "Re: random string"
- Next in thread: Gianni Mariani: "Re: Why does vector.end() point beyond last element?"
- Reply: Gianni Mariani: "Re: Why does vector.end() point beyond last element?"
- Reply: Bob R: "Re: Why does vector.end() point beyond last element?"
- Reply: George Huber: "Re: Why does vector.end() point beyond last element?"
- Reply: David White: "Re: Why does vector.end() point beyond last element?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Bob R: "Re: C++ and graphics. [OT]"
- Previous message: Mark : "Re: random string"
- Next in thread: Gianni Mariani: "Re: Why does vector.end() point beyond last element?"
- Reply: Gianni Mariani: "Re: Why does vector.end() point beyond last element?"
- Reply: Bob R: "Re: Why does vector.end() point beyond last element?"
- Reply: George Huber: "Re: Why does vector.end() point beyond last element?"
- Reply: David White: "Re: Why does vector.end() point beyond last element?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|