C++11中的shared_ptr案例

之前的博客基本是对算法仿真,写的我都吐了。现在是对C++11的shared_ptr做一个demo。
不说废话,下面是效果图:
这里写图片描述


/**
* 智能指针shared_ptr Demo代码
* 软件环境: Windows 10 + Visual Studio 2015
* 硬件环境: CPU i7-8750H; RAM 8G; Gpu GTX1050Ti
* 参考文献: PaulDeitel, HarveyDeitel, 戴特尔. C++11 for programmers[M]. 电子工业出版社, 2016.
* 参考代码: http://www.deitel.com/LinkClick.aspx?link=http%3a%2f%2fwww.informit.com%2fcontent%2fimages%2f9780133439854%2fsourcecode%2fexamples.zip&tabid=3648&mid=6936
* 参考网址: http://www.deitel.com/Books/C/C11forProgrammers2e/tabid/3648/Default.aspx
*/
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>

class Book
{
public:
    explicit Book(const std::string& bookTitle) :title(bookTitle) {}

    ~Book()
    {
        std::cout << "Destroying Book:" << title << std::endl;
    }

    std::string title;
};

typedef std::shared_ptr<Book> BookPtr;

/** @brief a custom delete function for a pointer to a Book

@param ptrBook  a pointer to a book
*/
static void DeleteBook(Book* ptrBook)
{
    std::cout << "Custom deleter for a Book" << std::endl;
    delete ptrBook;
}


/** @brief compare the titles of two Books for sorting

@param ptr1 a pointer to Book1
@param ptr2 a pointer to Book2
*/
static bool CompareTitles(BookPtr ptr1, BookPtr ptr2)
{
    return (ptr1->title < ptr2->title);
}

/** @brief print the Books in the vector

@param books Books Object in the vector
*/
static void PrintBookTitles(const std::vector<BookPtr>& books)
{
    for (int i = 0; i < books.size(); ++i)
    {
        std::cout << (books[i])->title << "\n";
    }
}

/** @brief 之所以将代码封装在此, 是想观察在作用域后, 类对象是否析构

即判断是否调用~Book::Book()函数
*/
static void shared_ptr_demo()
{
    // create a shared_ptr to a Book and display the reference count
    BookPtr bookPtr(new Book("C++ How to Program"));
    std::string title = bookPtr->title;
    long use_count = bookPtr.use_count();
    std::cout << "Reference count for Book " << bookPtr->title << " is: " << bookPtr.use_count() << std::endl;

    // create another shared_ptr to the Book and display reference count
    BookPtr bookPtr2(bookPtr);
    title = bookPtr->title;
    use_count = bookPtr.use_count();
    std::cout << "Reference count for Book " << bookPtr->title << " is: " << bookPtr.use_count() << std::endl;

    // change the Book's title and access it from both pointers
    bookPtr2->title = "Java How to Program";
    std::cout << "\nThe Book's title changed for both pointers: " << "\nbookPtr: " << bookPtr->title << "\nbookPtr2: " << bookPtr2->title << std::endl;

    // create a std::vector of shared_ptrs to Books (BookPtrs)
    std::vector< BookPtr > books;
    books.push_back(BookPtr(new Book("C How to Program")));
    books.push_back(BookPtr(new Book("VB How to Program")));
    books.push_back(BookPtr(new Book("C# How to Program")));
    books.push_back(BookPtr(new Book("C++ How to Program")));

    // print the Books in the vector
    std::cout << "\nBooks before sorting: " << std::endl;
    PrintBookTitles(books);

    // sort the vector by Book title and print the sorted vector
    std::cout << "\nBooks after sorting: " << std::endl;
    std::sort(books.begin(), books.end(), CompareTitles);
    PrintBookTitles(books);

    // create a shared_ptr with a custom deleter
    BookPtr bookPtr3(new Book("Small C++ How to Program"), DeleteBook);
    bookPtr3.reset(); // release the Book this shared_ptr manages

    // shared_ptrs are going out of scope
    std::cout << "\nAll shared_ptr objects are going out of scope." << std::endl;
}

void test_smart_pointer()
{
    shared_ptr_demo();
    int i = 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值