-
template <typename Alloc> void TestAlloc(Alloc&& alloc, int32_t count) { typedef Alloc::value_type ValueType; const size_t value_size = sizeof(ValueType); ValueType *p1{ nullptr }, *p2{ nullptr }, *p3{ nullptr }; p1 = alloc.allocate(count); p2 = alloc.allocate(count); p3 = alloc.allocate(count); std::cout << "p1:[" << (void*)p1 << "] p2:[" << (void*)p2 << "] p3:[" << (void*)p3 << "]\n"; alloc.deallocate(p1, value_size); alloc.deallocate(p2, value_size); alloc.deallocate(p3, value_size); } void TestAlloc() { TestAlloc(std::allocator<int32_t>(), 1); }
2022-05-06 14:58:15
-
vector删除元素会消耗额外的时间用于内存拷贝,删除尾部元素时消耗时间是常数值与大小无关。
xutility
4417-4434
_InIt _First, //需要删除元素的下一个元素
_InIt _Last, //end
_OutIt _Dest//需要删除的元素位置template <class _InIt, class _OutIt> _CONSTEXPR20 _OutIt _Move_unchecked(_InIt _First, _InIt _Last, _OutIt _Dest) { // move [_First, _Last) to [_Dest, ...) // note: _Move_unchecked has callers other than the move family if constexpr (_Ptr_move_cat<_InIt, _OutIt>::_Trivially_copyable) { #ifdef __cpp_lib_is_constant_evaluated if (!_STD is_constant_evaluated()) #endif // __cpp_lib_is_constant_evaluated { return _Copy_memmove(_First, _Last, _Dest); } } //删除一个元素时,会将要删除元素之后的所有元素都向前移一个位置 for (; _First != _Last; ++_Dest, (void) ++_First) { *_Dest = _STD move(*_First); } return _Dest; }
2022-05-01 14:16:29
-
vector扩容机制,不同厂商有不同的方式。
linux下以2倍扩容,vs下为1.5倍扩容
VS:
vector文件
1683-1699 row
重点为1629行:const size_type _Geometric = _Oldcapacity + _Oldcapacity / 2;
_CONSTEXPR20_CONTAINER size_type _Calculate_growth(const size_type _Newsize) const { // given _Oldcapacity and _Newsize, calculate geometric growth const size_type _Oldcapacity = capacity(); const auto _Max = max_size(); if (_Oldcapacity > _Max - _Oldcapacity / 2) { return _Max; // geometric growth would overflow } const size_type _Geometric = _Oldcapacity + _Oldcapacity / 2; if (_Geometric < _Newsize) { return _Newsize; // geometric growth would be insufficient } return _Geometric; // geometric growth is sufficient }
2022-05-01 13:53:11
-
malloc
32位程序8字节对齐
64位程序16字节对齐32位程序 malloc 自带8字节cookie
32位程序:
malloc(sizeof(char)*8) //实际占用16字节
malloc(sizeof(char)*9) //实际占用24字节2022-04-30 18:09:55
-
重载输出流运算符
template <typename _Type> std::ostream& operator<<(std::ostream& s, const Z<_Type>& z) { return s << z.Get(); } template <typename _Type> std::ostream& operator<<(std::ostream& s, const vector<Z<_Type>>& vec) { for (const auto& it : vec)s << it << " "; return s; }
2021-12-01 15:41:47
-
对于map,删除erase之后,只会影响到当前的iterator
for (auto iter = mmmap.begin(); iter != mmmap.end(); ) { if (iter->second % 2 == 0) { mmmap.erase(iter++); } else { ++iter; } }
2021-12-01 15:35:11
-
std::vector安全删除元素。
vector删除元素会导致迭代器失效,后面的迭代器未知。for (auto iter = findTest.begin(); iter != findTest.end();) { if (*iter % 2 == 0) { iter = findTest.erase(iter); } else { ++iter; } }
2021-12-01 15:32:04
-
bool operator()(const Z<_Type>& left, const Z<_Type>&right) const noexcept//仿函数
2021-11-11 09:13:38
-
operator _Type& () //隐式类型转换
2021-11-11 09:13:12
-
知足常乐
2021-08-26 16:02:13