C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- std::addressof
- std::allocator_traits
- std::unique_ptr
- std::scoped_allocator_adaptor
- std::auto_ptr
- std::destroy_at
- std::destroy
- std::destroy_n
- std::uninitialized_move
- std::uninitialized_value_construct
- std::owner_less
- std::shared_ptr
- std::to_address
- std::assume_aligned
- std::make_obj_using_allocator
- C å å管çåº
- ä½å±å å管ç
- std::pmr::memory_resource
- std::allocator
- std::allocator<T>::construct
- std::allocator<T>::destroy
- std::allocator<T>::allocate
- std::default_delete
- std::allocator_arg_t
- std::allocator_arg
- std::weak_ptr
- std::enable_shared_from_this
- std::bad_weak_ptr
- std::pointer_traits
- std::uses_allocator
- std::uses_allocator_construction_args
- std::uninitialized_construct_using_allocator
- std::pmr::polymorphic_allocator
- std::pmr::get_default_resource
- std::pmr::set_default_resource
- std::pmr::new_delete_resource
- std::pmr::null_memory_resource
- std::pmr::synchronized_pool_resource
- std::pmr::unsynchronized_pool_resource
- std::pmr::monotonic_buffer_resource
- std::pmr::pool_options
- std::raw_storage_iterator
- std::get_temporary_buffer
- std::return_temporary_buffer
- std::uninitialized_copy
- std::uninitialized_fill
- std::uninitialized_default_construct
- std::uninitialized_copy_n
- std::uninitialized_fill_n
- std::uninitialized_move_n
- std::uninitialized_default_construct_n
- std::uninitialized_value_construct_n
- std::construct_at
- std::align
- 注é
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >卿å å管ç > std::allocator
std::allocator
| å®ä¹äºå¤´æä»¶ <memory>
|
||
| template< class T > struct allocator; |
(1) | |
| template<> struct allocator<void>; |
(2) | (C++17 ä¸å¼ç¨) (C++20 ä¸ç§»é¤) |
std::allocator ç±»æ¨¡æ¿æ¯æææ ååºå®¹å¨æç¨çé»è®¤åé
å¨ (Allocator) ï¼è¥ä¸æä¾ç¨æ·æå®çåé
å¨ãé»è®¤åé
卿 ç¶æï¼å³ä»»ä½ç»å®ç allocator å®ä¾å¯äº¤æ¢ãæ¯è¾ç¸çï¼ä¸è½è§£åé
åä¸ allocator ç±»åçä»»ä½å
¶ä»å®ä¾æåé
çå
åã
|
对 void çæ¾å¼ç¹åç¼ºå°æå typedef |
(C++20 å) |
| ææèªå®ä¹åé å¨å¿ 须乿 ç¶æã | (C++11 å) |
| èªå®ä¹åé å¨å¯ä»¥å«æç¶æãæ¯ä¸ªå®¹å¨æå ¶ä»å ·åé å¨å¯¹è±¡åå¨ä¸ä¸ªæä¾çåé å¨å®ä¾ï¼å¹¶éè¿ std::allocator_traits æ§å¶åé 卿¿æ¢ã | (C++11 èµ·) |
| é»è®¤åé 卿»¡è¶³åé å¨å®æ´æ§è¦æ±ã | (C++17 èµ·) |
æåç±»å
| Â | |
| ç±»å | å®ä¹ |
value_type
|
T |
pointer (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
T* |
const_pointer (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
const T* |
reference (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
T& |
const_reference (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
const T& |
size_type
|
std::size_t |
difference_type
|
std::ptrdiff_t |
propagate_on_container_move_assignment(C++14)
|
std::true_type |
rebind (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
template< class U > struct rebind { typedef allocator<U> other; }; |
is_always_equal(C++17)
|
std::true_type |
æå彿°
| å建æ°ç allocator å®ä¾ (å ¬å¼æå彿°) | |
| ææ allocator å®ä¾ (å ¬å¼æå彿°) | |
| (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤) |
è·å¾å¯¹è±¡çå°åï¼å³ä½¿éè½½äº operator& (å ¬å¼æå彿°) |
| åé
æªåå§åçåå¨ (å ¬å¼æå彿°) | |
| è§£åé
åå¨ (å ¬å¼æå彿°) | |
| (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤) |
è¿åæå¤§ç忝æåé
å¤§å° (å ¬å¼æå彿°) |
| (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤) |
å¨åé
çå卿é 对象 (å ¬å¼æå彿°) |
| (C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤) |
ææå¨å·²åé
åå¨ä¸ç对象 (å ¬å¼æå彿°) |
éæå彿°
| (C++20 ä¸ç§»é¤) |
æ¯è¾ä¸¤ä¸ªåé
å¨å®ä¾ (å ¬å¼æå彿°) |
注æ
æåæ¨¡æ¿ rebind æä¾è·å¾ä¸åç±»åç allocator çæ¹å¼ãä¾å¦ï¼
std::list<T, A> ç¨åé
å¨ A::rebind<Node<T>>::other åé
æä¸ªå
é¨ç±»å Node<T> ç»ç¹
|
(C++11 å) |
std::list<T, A> ç¨åé
å¨ std::allocator_traits<A>::rebind_alloc<Node<T>> åé
æä¸ªå
é¨ç±»å Node<T> ç»ç¹ï¼è¿ç¨ A::rebind<Node<T>>::other å®ç°ï¼è¥ A æ¯ std::allocator
|
(C++11 èµ·) |
示ä¾
è¿è¡æ¤ä»£ç
#include <memory> #include <iostream> #include <string>  int main() { std::allocator<int> a1; // int çé»è®¤åé å¨ int* a = a1.allocate(1); // ä¸ä¸ª int çç©ºé´ a1.construct(a, 7); // æé int std::cout << a[0] << '\n'; a1.deallocate(a, 1); // è§£åé ä¸ä¸ª int çç©ºé´ Â // string çé»è®¤åé å¨ std::allocator<std::string> a2;  // åä¸ï¼ä½ä»¥ a1 çéç»å®è·å decltype(a1)::rebind<std::string>::other a2_1;  // åä¸ï¼ä½éè¿ allocator_traits ç±ç±»å a1 çéç»å®è·å std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2;  std::string* s = a2.allocate(2); // 2 个 string çç©ºé´ Â a2.construct(s, "foo"); a2.construct(s + 1, "bar");  std::cout << s[0] << ' ' << s[1] << '\n';  a2.destroy(s); a2.destroy(s + 1); a2.deallocate(s, 2); }
è¾åºï¼
7 foo bar
åé
| (C++11) |
æä¾å
³äºåé
å¨ç±»åçä¿¡æ¯ (类模æ¿) |
| (C++11) |
为å¤çº§å®¹å¨å®ç°çå¤çº§åé
å¨ (类模æ¿) |
| (C++11) |
æ£æ¥æå®çç±»åæ¯å¦æ¯æä½¿ç¨åé
å¨çæé (类模æ¿) |