向量::在C 中分配工作
How does vector::assign work in C++?
如果我有以下代码:
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));
}
}
以及更多...
- 为什么它在不分配内存的情况下工作正常
- C++:检查动态取消分配是否正常工作
- 虚函数如何工作,分配后新的返回类型会发生什么?
- 为什么必须动态分配扩展数组才能使此功能正常工作C++
- 将正常函数的工作分配给多个线程是否安全
- 在不工作的情况下为数组分配指针,但反过来也可以
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 使用 std::map 的递归堆栈分配如何工作?
- 不工作 复制分配运算符
- 返回对象如何与分配运算符一起工作
- 如果不允许我分配 rvalues 来引用为什么以下代码片段有效,这在内部如何工作?
- 向量::在C 中分配工作
- 将工作与固定数量的螺纹之间的工作和pthread之间的分配
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- OpenCL:动态内存分配,是使用空闲工作项更好还是同时写入更好
- C++动态内存分配未按预期工作
- 如果堆分配的对象被销毁并且指针被重新分配,Qt的信号和插槽系统会工作吗?
- 为什么安置新员工比直接分配工作快得多?
- 将c实现的静态分配工作区转换为c++
- 不同类型的指针之间的分配工作,我不知道为什么