简体   繁体   中英

default constructor of stl vector

the signature of default constructor is

explicit vector (const allocator_type& alloc = allocator_type());

How to read this? Why is it not prefixed by a

template <class ....>

what does the first and only argument

const allocator_type& alloc = allocator_type()

mean?

are all stl container classes already prefixed by a template <class ...>

How does it define the type used for every element of a vector if its not followed by a template instruction?

Can anybody show an example using the default method?

Why is it not prefixed by a template <class...> ?

Because it's inside the definition of the vector class template. Inside the class template, you don't specify that each member is also a template. Adding a template specification to a member would give it extra template parameters, in addition to the class parameters, which aren't needed for this constructor.

what does the first and only argument mean?

That's an optional argument, which can be used to supply a custom memory allocator to give you more control over how the vector uses memory. Normally, you'd miss that argument out and get the default value. The default is an instance of vector<T>::allocator_type , which is an alias for std::allocator<T> . This will allocate memory from the free store, using the global new and delete operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM