Constructor构造函数
语法:
1 2 3 4 5 6 7 8 9 10 11 |
1.空向量 explicit vector(const allocator_type& __a = allocator_type()); 2.指定向量元素个数 explicit vector(size_type __n); 3.拷贝构造 vector(const vector& __x); 4.指定向量元素个数,并初始化元素值 vector(size_type __n, const value_type& __value, const allocator_type& __a = allocator_type()); 5.用迭代器构造向量 template<typename _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) |
构造函数
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// (assign() and get_allocator() are also listed in this section) /** * @brief Default constructor creates no elements. */ explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) { } /** * @brief Create a %vector with copies of an exemplar element. * @param n The number of elements to initially create. * @param value An element to copy. * * This constructor fills the %vector with @a n copies of @a value. */ vector(size_type __n, const value_type& __value, const allocator_type& __a = allocator_type()) : _Base(__n, __a) { this->_M_impl._M_finish = std::uninitialized_fill_n(this->_M_impl._M_start, __n, __value); } /** * @brief Create a %vector with default elements. * @param n The number of elements to initially create. * * This constructor fills the %vector with @a n copies of a * default-constructed element. */ explicit vector(size_type __n) : _Base(__n, allocator_type()) { this->_M_impl._M_finish = std::uninitialized_fill_n(this->_M_impl._M_start, __n, value_type()); } /** * @brief %Vector copy constructor. * @param x A %vector of identical element and allocator types. * * The newly-created %vector uses a copy of the allocation * object used by @a x. All the elements of @a x are copied, * but any extra memory in * @a x (for fast expansion) will not be copied. */ vector(const vector& __x) : _Base(__x.size(), __x.get_allocator()) { this->_M_impl._M_finish = std::uninitialized_copy(__x.begin(), __x.end(), this->_M_impl._M_start); } /** * @brief Builds a %vector from a range. * @param first An input iterator. * @param last An input iterator. * * Create a %vector consisting of copies of the elements from * [first,last). * * If the iterators are forward, bidirectional, or * random-access, then this will call the elements' copy * constructor N times (where N is distance(first,last)) and do * no memory reallocation. But if only input iterators are * used, then this will do at most 2N calls to the copy * constructor, and logN memory reallocations. */ template<typename _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { // Check whether it's an integral type. If so, it's not an iterator. typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_initialize_dispatch(__first, __last, _Integral()); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { //explicit vector(const allocator_type& __a = allocator_type()); vector<int> Veci1; //explicit vector(size_type __n); vector<int> Veci2(10); //vector(const vector& __x); vector<int> Veci3(Veci2); //vector(size_type __n, const value_type& __value, const allocator_type& __a = allocator_type()); vector<int> Veci4(10, 1); //template<typename _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) vector<int> Veci5(Veci4.begin(), Veci4.end()); } |