|
-
July 18th, 2002, 04:00 AM
#1
How to get the iterator of the last element?
Except using rbegin, are there any other stl functions for getting the iterator of the last element? vector::back just gives me the reference of that.
In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".
-
July 18th, 2002, 04:11 AM
#2
--c.end().
Only works for Bidirectional containers but, then, so does c.rbegin().
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 04:30 AM
#3
Graham,
Isn't c.end () one past the last one?
Succinct is verbose for terse
-
July 18th, 2002, 04:36 AM
#4
That's why I applied the decrement operator to it.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 04:41 AM
#5
Sorry - I thought the -- was a comment. Ada is getting me confused.
Succinct is verbose for terse
-
July 18th, 2002, 04:56 AM
#6
If i use decrement operator on the end iterator, will the end iterator "itself" decrease by 1?
Just like
int i = 0;
--i;
Then i will become -1.
May be this is a silly question, sorry.
In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".
-
July 18th, 2002, 05:27 AM
#7
No.
end() simply returns a one-past-the-end iterator and it's that that you decrement.
Code:
std::vector<int> v;
// fill v...
std::vector<int>::iterator i = --v.end();
// i now points to the last element in v.
It would probably be a good idea to check that the container is not empty(), though. Decrementing end() then could cause problems.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 07:18 AM
#8
Oops, VC6 can't compile it.
error C2105: '--' needs l-value
In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".
-
July 18th, 2002, 07:36 AM
#9
OK. Get the interator into a variable and decrement that:
Code:
(whatever)::iterator i = c.end();
--i;
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 07:51 AM
#10
Thanks
In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".
-
July 18th, 2002, 11:10 AM
#11
You can also writefor vector iterators.
Ce n'est que pour vous dire ce que je vous dis.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|