• c++11 智能指针 (std::shared_ptr)(四)


      定义于头文件 
    template< class T > class shared_ptr;     (C++11 起) 

    返回指定类型中的删除器,若其拥有

    std::get_deleter
    1. template< class Deleter, class T >
    2. Deleter* get_deleter( const std::shared_ptr<T>& p ) noexcept; (C++11 起)

    访问 p 的删除器。若共享指针 p 占有无 cv 限定 Deleter 类型的删除器(例如,若它以接收删除器为参数的构造函数之一创建),则返回指向删除器的指针。否则,返回空指针

    参数

    p-需要访问其删除器的共享指针

    返回值

    指向被占有删除器的指针或 nullptr 。只要至少还有一个 shared_ptr 实例占有返回的指针,它就合法。

    注意

    返回的指针可能比最后一个 shared_ptr 的生存期更持久,例如,还剩下 std::weak_ptr 时且实现在销毁整个控制块前不销毁删除器。

    调用示例

    1. #include <iostream>
    2. #include <memory>
    3. struct Foo
    4. {
    5. int i;
    6. };
    7. void foo_deleter(Foo * p)
    8. {
    9. std::cout << "foo_deleter called!\n";
    10. delete p;
    11. }
    12. int main()
    13. {
    14. std::shared_ptr<int> aptr;
    15. {
    16. // 创建拥有一个 Foo 和删除器的 shared_ptr
    17. auto foo_p = new Foo;
    18. std::shared_ptr<Foo> r(foo_p, foo_deleter);
    19. aptr = std::shared_ptr<int>(r, &r->i); // 别名使用构造函数
    20. // aptr 现在指向 int ,但管理整个 Foo
    21. } // r 被销毁(不调用删除器)
    22. // 获得指向删除器的指针:
    23. if (auto del_p = std::get_deleter<void(*)(Foo*)>(aptr))
    24. {
    25. std::cout << "shared_ptr owns a deleter\n";
    26. if (*del_p == foo_deleter)
    27. {
    28. std::cout << "...and it equals &foo_deleter\n";
    29. }
    30. }
    31. else
    32. {
    33. std::cout << "The deleter of shared_ptr is null!\n";
    34. }
    35. } // 于此调用删除器

    示例

     
    与另一个 shared_ptr 或 nullptr 进行比较

    std::shared_ptr<T>::operator==, !=, <, <=, >, >=

    比较二个 shared_ptr 对象

    template < class T, class U >
    bool operator==( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (1)(C++11 起)

    template< class T, class U >
    bool operator!=( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (2)(C++11 起)

    template< class T, class U >
    bool operator<( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (3)(C++11 起)

    template< class T, class U >
    bool operator>( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (4)(C++11 起)

    template< class T, class U >
    bool operator<=( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (5)(C++11 起)

    template< class T, class U >
    bool operator>=( const shared_ptr& lhs, const shared_ptr& rhs ) noexcept;

    (6)(C++11 起)

    比较 shared_ptr 与空指针 

    template< class T >
    bool operator==( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (7)(C++11 起)

    template< class T >
    bool operator==( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (8)(C++11 起)

    template< class T >
    bool operator!=( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (9)(C++11 起)

    template< class T >
    bool operator!=( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (10)(C++11 起)

    template< class T >
    bool operator<( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (11)(C++11 起)

    template< class T >
    bool operator<( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (12)(C++11 起)

    template< class T >
    bool operator>( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (13)(C++11 起)

    template< class T >
    bool operator>( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (14)(C++11 起)

    template< class T >
    bool operator<=( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (15)(C++11 起)

    template< class T >
    bool operator<=( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (16)(C++11 起)

    template< class T >
    bool operator>=( const shared_ptr& lhs, std::nullptr_t rhs ) noexcept;

    (17)(C++11 起)

    template< class T >
    bool operator>=( std::nullptr_t lhs, const shared_ptr& rhs ) noexcept;

    (18)(C++11 起)

    比较二个 shared_ptr 对象或将 shared_ptr 与空指针比较。

    注意 shared_ptr 的比较运算符简单地比较指针值;比较所指向的实际对象。对 shared_ptr 定义 operator< 允许将 shared_ptr 用作关联容器,如 std::map 与 std::set 中的关键。

    参数

    lhs-要比较的左侧 shared_ptr
    rhs-要比较的右侧 shared_ptr

    返回值

    1) lhs.get() == rhs.get()

    2) !(lhs == rhs)

    3) std::less()(lhs.get(), rhs.get()) ,其中 V 是 std::shared_ptr::element_type* 与 std::shared_ptr::element_type* 的合成指针类型

    4) rhs < lhs

    5) !(rhs < lhs)

    6) !(lhs < rhs)

    7) !lhs

    8) !rhs

    9) (bool)lhs

    10) (bool)rhs

    11) std::less::element_type*>()(lhs.get(), nullptr)

    12) std::less::element_type*>()(nullptr, rhs.get())

    13) nullptr < lhs

    14) rhs < nullptr

    15) !(nullptr < lhs)

    16) !(rhs < nullptr)

    17) !(lhs < nullptr)

    18) !(nullptr < rhs)

    注意

    所有情况下,被比较者是存储的指针( get() 所返回者)而非被管理指针( uses_count 达到零时传递给删除器者)。二个指针可能在用别名使用构造函数创建的 shared_ptr 中不同。

    将存储的指针的值输出到输出流

    std::shared_ptr<T>::operator<<
    1. template <class T, class U, class V>
    2. std::basic_ostream<U, V>& operator<<
    3. (std::basic_ostream<U, V>& os, const std::shared_ptr<T>& ptr);

    插入存储于 ptr 的指针值到输出流 os 中。

    等价于 os << ptr.get()

    参数

    os-要插入 ptr 到的 std::basic_ostream
    ptr-被插入到 os 的数据

    返回值

    os

    调用示例

    1. #include <iostream>
    2. #include <memory>
    3. class Foo {};
    4. int main()
    5. {
    6. auto sp = std::make_shared<Foo>();
    7. std::cout << sp << std::endl;
    8. std::cout << sp.get() << std::endl;
    9. }

    输出


    特化 std::swap 算法

    std::swap(std::shared_ptr)
    1. template< class T >
    2. void swap( shared_ptr<T>& lhs, shared_ptr<T>& rhs ) noexcept;(C++11 起)

    为 std::shared_ptr 特化 std::swap 算法。交换 lhsrhs 的指针。调用 lhs.swap(rhs) 。

    参数

    lhs, rhs-要交换内容的智能指针

    返回值

    (无)

    复杂度

    常数

    std::shared_ptr 的散列支持

    std::hash(std::shared_ptr)
    template<class T> struct hash<shared_ptr<T>>;  (C++11 起) 

    std::hash 对 std::shared_ptr 的模板特化允许用户获得 std::shared_ptr 类型对象的哈希。

    对于给定的 std::shared_ptr p ,此特化确保

    std::hash>()(p) == std::hash()(p.get()).

    (C++17 前)

    std::hash>()(p) == std::hash::element_type*>()(p.get()).

    (C++17 起)
  • 相关阅读:
    一个EDC系统的架构设计方案
    Redis面试篇
    2023年中国铝压延产量、销售收入及市场规模分析[图]
    简单配置linux防火墙
    Pycharm配置Git以及Gitee实现代码管理(全网最详细)
    RSCMVR
    Hadoop之小文件问题及解决方案
    Prime Sample
    选择题汇总6-7(括号里填的答案都是对的,不用管下面那个答案正确与错误,因为作者懒得删了)
    【学习笔记】CF1651F Tower Defense
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/126696549