in the vector in stl.

Recommended Posts

vector<BYTE> in (4096); and the pointer of in head is not the in? I must use &in[0] to get the head pointer?

Share on other sites
what pointer? what head? what does the 4096 mean?

Share on other sites
Quote:
 Original post by Anonymous Posterwhat pointer? what head? what does the 4096 mean?

I wonder if he's got confused between vectors and linked lists?

Share on other sites
I mean first address of a vector/list is just the name of vector.

but vector in stl may be exception.

Share on other sites
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 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 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 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 on other sites
Quote:
 Original post by RDragon1What'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 on other sites
Quote:
 Original post by dalleboyThere 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 dalleboyexcept for the ugly std::vector specialization, which shouldn't be used anyway

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

Share on other sites
Quote:
 Original post by snk_kidNothing 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>

Create an account

Register a new account

• Forum Statistics

• Total Topics
628383
• Total Posts
2982384

• 10
• 9
• 15
• 24
• 11