N
Numeromancer
From the C++-FAQ Lite:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3
----------------------------
34.3] Is the storage for a std::vector<T> guaranteed to be contiguous? Yes.
This means you the following technique is safe:
#include <vector>
#include "Foo.h" /* get class Foo */
// old-style code that wants an array
void f(Foo* array, unsigned numFoos);
void g()
{
std::vector<Foo> v;
...
f(v.empty() ? NULL : &v[0], v.size()); ↠safe
}
The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first (zeroth)
element of v. If you know a priori that v is not empty, you can change
that to simply &v[0].
----------------------------
I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?
Thanks,
Tim S.
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3
----------------------------
34.3] Is the storage for a std::vector<T> guaranteed to be contiguous? Yes.
This means you the following technique is safe:
#include <vector>
#include "Foo.h" /* get class Foo */
// old-style code that wants an array
void f(Foo* array, unsigned numFoos);
void g()
{
std::vector<Foo> v;
...
f(v.empty() ? NULL : &v[0], v.size()); ↠safe
}
The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first (zeroth)
element of v. If you know a priori that v is not empty, you can change
that to simply &v[0].
----------------------------
I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?
Thanks,
Tim S.