I know you can just do: &theVector[0], but is this standard? Is this behavior always guaranteed?
If not, is there a better, less ‘hackish’ way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, that behavior is guaranteed. Although I can’t quote it, the standard guarantees that vector elements are stored consecutively in memory to allow this.
There is one exception though:
It will not work for
vector<bool>because of a template specialization.http://en.wikipedia.org/wiki/Sequence_container_%28C%2B%2B%29#Specialization_for_bool
This specialization attempts to save memory by packing
boolstogether in a bit-field. However, it breaks some semantics and as such,&theVector[0]on avector<bool>will not work.In any case,
vector<bool>is widely considered to be a mistake so the alternative is to usestd::deque<bool>instead.