向量::在C 中分配工作

How does vector::assign work in C++?

本文关键字:分配 工作 向量      更新时间:2023-10-16

如果我有以下代码:

std::vector<char> myvector;
char myarr[10];
myvector.reserve(10);
myvector.assign(myarr, myarr + 3);

如果我不考虑更新会员变量或调整大小,则最后一行(分配)用作 memcpy(&amp; myvector [0],myarr,3)

好吧,让我们做这个

定义:http://www.cplusplus.com/reference/cstring/memcpy/

复制内存块从位置复制num字节的值 直接指向源指向的内存块 目的地。

源和源指向的对象的基本类型 目的地指针与此功能无关。结果是 数据的二进制副本。

该函数不会检查任何终止的null字符 来源 - 它始终将其恰好复制数字字节。

为了避免溢出,阵列的大小都指向 目的地和源参数至少应为num字节,并且 不应重叠(对于重叠的内存块,memmove是更安全的 方法)。

现在是向量:http://www.cplusplus.com/reference/vector/Vector/Vector/

向量是代表可以更改的数组的序列容器 尺寸。

就像数组一样,向量使用连续的存储位置为其 元素,这意味着他们的元素也可以使用 定期指示的元素偏移,并同样有效 就像数组一样。但是与数组不同,它们的大小可以动态变化, 由于容器会自动处理存储。

让我们深入研究一些实现,您会看到分配是一切,但是类似于memcpy,即使您不考虑调整大小和成员变量更新...)

当然...它与新需要的元素相比执行更大或较小的元素的副本,但进行内存 - 实现,界限和有效性检查等。

...

memcpy只是复制一个字节,很高兴。向量复制全元素。

为了您的兴趣:

GCC接口

https://gcc.gnu.org/onlinedocs/gcc-4.6.3/libstdc /api/a01115_source.html

template<typename _InputIterator>
  void
    assign(_InputIterator __first, _InputIterator __last)
    {
        __glibcxx_check_valid_range(__first, __last);
        **_Base::assign(__gnu_debug::__base(__first),
                      __gnu_debug::__base(__last));**
         this->_M_invalidate_all();
         _M_update_guaranteed_capacity();
    }
  void
    assign(size_type __n, const _Tp& __u)
    {
        _Base::assign(__n, __u);
        this->_M_invalidate_all();
        _M_update_guaranteed_capacity();
    }
#ifdef __GXX_EXPERIMENTAL_CXX0X__
  void
    assign(initializer_list<value_type> __l)
    {
        _Base::assign(__l);
        this->_M_invalidate_all();
        _M_update_guaranteed_capacity();
    }
#endif

_BASE ::分配

https://gcc.gnu.org/onlinedocs/libstdc /libstdc html-users-3.4/stl__vector_8h-source.html

void
  assign(size_type __n, const value_type& __val)
  { _M_fill_assign(__n, __val); }

template<typename _InputIterator>
  void
    assign(_InputIterator __first, _InputIterator __last)
    {
        // Check whether it's an integral type.  If so, it's not an iterator.
        typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
        _M_assign_dispatch(__first, __last, _Integral());
    }

m - 实施(并非所有需要的都是)...

https://gcc.gnu.org/onlinedocs/gcc-4.6.3/libstdc /api/a01117_source.html

template<typename _Tp, typename _Alloc>
  void
    vector<_Tp, _Alloc>::
       _M_fill_assign(size_t __n, const value_type& __val)
    {
        if (__n > capacity())
        {
            vector __tmp(__n, __val, _M_get_Tp_allocator());
            __tmp.swap(*this);
        }
        else if (__n > size())
        {
            std::fill(begin(), end(), __val);
            std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
                                          __n - size(), __val,
                                          _M_get_Tp_allocator());
            this->_M_impl._M_finish += __n - size();
        }
        else
            _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
        }
    }

以及更多...