Goran Pusic wrote:
>
> Given:
> std::vector<TYPE> v;
> void process(size_t count, TYPE* data);
>
> Is there any problem if I do
>
> if (!v.empty())
> {
> std::vector<TYPE>::reference r = *v.begin();
> // or
> r = v[0];
> // or
> r = v.at(0);
> process_raw(v.size(), &p);
> }
>
> ?
Yes, the technique it is perfectly valid.
You can simply use `&v.front()` to obtain the pointer to the first
element of the raw array, instead of going through the iterators and all
these intermediate dereferences and address-takings.
Also, be aware of specialized versions of `std::vector<>`, like
`std::vector<bool>` for example, which will not work with this technique
(it stores bool values in a packed array of bits, providing no direct
access to the raw array).
--
Best regards,
Andrey Tarasevich
|