Sign in to follow this  
derek7

in the vector in stl.

Recommended Posts

Yes, you'll most likely need to take the address of the first element.

I don't think STL vector has an overload for the cast operator... I shouldn't say for sure, try and fail, and tell us if I'm wrong.

Share this post


Link to post
Share on other sites
Correct.

Since vector<> is a class and doesn't provide an overloaded operator for such a pointer, you actually need to write &in[0] to obtain a pointer to the first element of the vector.

-Markus-

Share this post


Link to post
Share on other sites
There is (yet) no guarantee that the elements in a std::vector are allocated in a continuous chunk of memory (according to the C++ standard), but in practice this is true in all implementations (except for the ugly std::vector<bool> specialization, which shouldn't be used anyway). One thing to take extra care about is an empty std::vector, these (somewhat handy) functions can perhaps be of some help:

template <typename T, typename A>
inline const T* c_ptr(const std::vector<T, A>& v)
{
return v.empty() ? 0 : &v[0];
}

template <typename T, typename A>
inline T* c_ptr(std::vector<T, A>& v)
{
return v.empty() ? 0 : &v[0];
}

Share this post


Link to post
Share on other sites
I don't understand why that code is useful... What's wrong with &vec[0] on an empty vector? You are likely going to query the vector's size anyway, so you would discover that the size is zero...



http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html

Item 69

Status: TC - (Technical Corrigenda) - The full WG21 committee has voted to accept the Defect Report's Proposed Resolution as a Technical Corrigenda. Action on this issue is thus complete and no further action is possible under ISO rules.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
What's wrong with &vec[0] on an empty vector?

int* p = NULL; // Legal.
int& r = *p; // Illegal.

vec[0] returns a reference to the first element in the vector, and what is that if the vector is empty?

Share this post


Link to post
Share on other sites
Quote:
Original post by dalleboy
There is (yet) no guarantee that the elements in a std::vector are allocated in a continuous chunk of memory (according to the C++ standard)


As of C++ 2003 Technical Corrigendum 1 (TC1) elements of std::vector are guaranteed to be stored contiguously.

Quote:
Original post by dalleboy
except for the ugly std::vector<bool> specialization, which shouldn't be used anyway


Nothing wrong with using it, it just has issues [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Nothing wrong with using it, it just has issues [smile].

As long as you know the issues, sure... [wink]
<JamesEarlJonesVoice>It is an abomination and must be destroyed!</JamesEarlJonesVoice>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this