[Effective-cpp] Item 1: Uses and Abuses of vector
Bronek Kozicki
brok at rubikon.pl
Sat Oct 23 09:19:26 EDT 2004
Balog Pal wrote:
> A good advice, though didn't help me out. :) Can someone give a real-life
> case when using at() is good? I mean in a production environment.
I had such case. One part of program was filling container with data,
and the other was using this data. These parts were weakly coupled (I do
not remember many details). The point is that if some number (determined
at runtime) of data has not been inserted, that was invalid input data
(but still valid program state) - program would need to throw exception
when trying to use non-existing data. Of course I could have checked
size() and then throw my own exception, but I preffered to re-use what's
already provided in std::vector.
> As i recall my use of containers, I either accessed an element that was
> (supposed to be) there, or the access created the possibly missing element
your second case sounds more like map<int, T>
> I' a big fan of const but for this situation I recall Scott Meyers had
> reservations on using const_iterator in ESTL.
I believe that's something under discussion of the C++ committee -
there's proposal to add cbegin, cend, crbegin and crend to allow
returning const_iterator and const_reverse_iterator from non-const
container, without explicit const_cast to enforce its constness. More
details at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1674.pdf
> for(vector<int>::iterator i = vec.begin(), endI = vec.end(); i != endI; ++i)
nice, but lack of "const" troubles me - I know this language limitation,
anyway. I also do not like for_each, for reasons stated by Paul. More
general reason is that std::for_each, like std::transform, can be used
to do anything with your container(s) - I do not like that this
"anything" is not visible in place where it actually counts. If local
class could be used as a template parameter (again, I believe it's under
committee's consideration) I would not have such problem.
B.
More information about the Effective-cpp
mailing list