之前的博客基本是对算法仿真,写的我都吐了。现在是对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;
}