std::vector use [0] or front()

Started by
11 comments, last by MaulingMonkey 15 years, 7 months ago
I like front(), it's more explicit and that's a good thing, and so it the commonality of it in other containers too. Also, in a debug build, operator[]() might perform bound checking with any associated performance penalty.
Advertisement
Quote:Original post by dmatter
I like front(), it's more explicit and that's a good thing, and so it the commonality of it in other containers too. Also, in a debug build, operator[]() might perform bound checking with any associated performance penalty.

It might do bounds checking in release builds, too. It is on by default in Visual C++. :)

The standard only says that at() is required to do the boundary check. It doesn't say that operator[] is required to not perform the check.
Quote:Original post by frob
The standard only says that at() is required to do the boundary check. It doesn't say that operator[] is required to not perform the check.


That said, I among others consider MSVC's vector::operator[] release mode checks that are on by default to be completely counter to C++'s philosophy of "You don't pay for what you don't use".

I tend to just use v[0], but I do use v.back(). The main case where I'd use .front() would be when I'm doing something special with that side and trying to contrast it against more general case code using v[N].

This topic is closed to new replies.

Advertisement