C + + STL vector Array (actually a pointer to an array)

Source: Internet
Author: User

Turn from: http://www.cnblogs.com/loongfee/archive/2012/05/05/2484574.html

Std::vector is convenient, but sometimes the parameters of the function called are arrays, you need to convert the vector to an array, and another space to make the vector a copy of the past too expensive, you can use the following methods.

Given a

Vector<int> v;
Expression V[0] produces a reference to the first element in the vector, so &v[0] is a pointer to that first element. The elements in the vector are limited by the C + + standard to be stored in contiguous memory, like an array, so if we want to pass V to this C-style API:

void dosomething (const int* pints, size_t numints);
We can do this:

DoSomething (&v[0], v.size ());
Well, maybe. It's possible. The only problem is that if V is empty. If so, V.size () is 0, and &v[0] tries to produce a pointer to something that does not exist at all. This is not a good thing. The result is undefined. A safer way to do this is to:

if (!v.empty ()) {
DoSomething (&v[0], v.size ());
}

Attention:

  If you are in a bad environment, you may encounter some half-baked characters, they will tell you that you can use V.begin () instead of &v[0], because (these annoying guys will tell you) begin to return to the vector inside the iterator, For vectors, the iterator is actually a pointer. That's often true, but as clause 50 says, it's not always the case, and you shouldn't rely on it. The return type of begin is iterator, not a pointer, and you should never use begin when you need a pointer to the vector's internal data. If you decide to type V.begin () for some reason, you should type &*v.begin (), because it will produce the same pointers as &v[0], allowing you to have more typing opportunities and making others feel more obscure to understand your code. Frankly, if you're dealing with someone who tells you to use V.begin () instead of &v[0, you should reconsider your social circle. In VC6, if you use V.begin () instead of &v[0], the compiler will not say anything, but if you do so in VC7 and g++, a compilation error will be raised  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.