std::vector capacity after copying

后端 未结 5 507
悲&欢浪女
悲&欢浪女 2020-12-02 01:40
  • Does vector::operator= change vector capacity? If so, how?
  • Does vector\'s copy constructor copy capacity?

I looked through documentation but c

相关标签:
5条回答
  • 2020-12-02 02:05

    Does vector::operator= change vector capacity? If so, how?

    It might change capacity. This happens only if the previous capacity was too small to hold the new size. If so, the new capacity is at least equal to the new size, but could be a larger value.

    Does copy constructor copy capacity?

    Per Table 65 Container requirements in C++03, X u (a); and X u = a; are both equivalent to X u; u = a;. This makes the copy ctor identical to the op= case, after default constructing the vector.

    0 讨论(0)
  • 2020-12-02 02:14
    1. As SGI STL vector soruce code shows below, operator= will reserve space for exactly n elements, i.e. _M_end_of_storage = _M_start + __xlen;.
        template <class _Tp, class _Alloc>
        vector<_Tp,_Alloc>&
        vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x)
        {
          if (&__x != this) {
            const size_type __xlen = __x.size();
            if (__xlen > capacity()) {
              iterator __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
              destroy(_M_start, _M_finish);
              _M_deallocate(_M_start, _M_end_of_storage - _M_start);
              _M_start = __tmp;
              _M_end_of_storage = _M_start + __xlen;
            }
            else if (size() >= __xlen) {
              iterator __i = copy(__x.begin(), __x.end(), begin());
              destroy(__i, _M_finish);
            }
            else {
              copy(__x.begin(), __x.begin() + size(), _M_start);
              uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
            }
            _M_finish = _M_start + __xlen;
          }
          return *this;
        }
    
    1. As SGI STL vector soruce code shows below, vector's copy constructor will reserve space for exactly n elements, i.e. _M_end_of_storage = _M_start + __n;.
          template <class _InputIterator>
          vector(_InputIterator __first, _InputIterator __last,
                 const allocator_type& __a = allocator_type()) : _Base(__a) {
            typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
            _M_initialize_aux(__first, __last, _Integral());
          }
    
          template <class _Integer>
          void _M_initialize_aux(_Integer __n, _Integer __value, __true_type) {
            _M_start = _M_allocate(__n);
            _M_end_of_storage = _M_start + __n;
            _M_finish = uninitialized_fill_n(_M_start, __n, __value);
          }
    
    0 讨论(0)
  • 2020-12-02 02:24

    As i wrote before, the copy need not - and usually DOES NOT - retain the capacity of the original vector.

    gcc version 4.1.1
    
    $ cat vt.cpp
    #include <vector>
    #include <iostream>
    int main() {
       std::vector<int> v1;
       v1.reserve(50000);
       std::vector<int> v2 = v1;
       std::cout << v1.capacity() << std::endl;
       std::cout << v2.capacity() << std::endl;
       return 0;
    }
    
    $ g++ vt.cpp -o vt && ./vt
    50000
    0
    
    $ cat v2.cpp
    #include <vector>
    #include <iostream>
    int main() {
       std::vector<int> v1;
       v1.reserve(50000);
       std::vector<int> v2;
       v2 = v1;
       std::cout << v1.capacity() << std::endl;
       std::cout << v2.capacity() << std::endl;
       return 0;
    }
    
    $ g++ v2.cpp -o v2 && ./v2
    50000
    0
    
    0 讨论(0)
  • 2020-12-02 02:30

    All you're guaranteed is that:

    1. The vector has enough capacity to store its elements. (Obviously.)
    2. The vector won't get a new capacity until it's current capacity is full.*

    So how much extra or little an implementation wants to put is up to the implementation. I think most will make capacity match size, when copying, but it cannot lower capacity. (Because of number 2 above; reallocating while there's enough room is not allowed.)

    * Mostly. See Charles' comments below.

    0 讨论(0)
  • 2020-12-02 02:32

    It is implementation dependent. Most in practice shrink the vectors to the minimum size.

    0 讨论(0)
提交回复
热议问题