C++ Boost PointerContainer智能指針詳解
一、提要
在 C++11 中,Boost.PointerContainer是另一個(gè)智能指針,一般是用來生成集合數(shù)據(jù)的,本文闡述這種指針的特點(diǎn)和用法。
二、智能指針Boost.PointerContainer
庫 Boost.PointerContainer 提供專門用于管理動(dòng)態(tài)分配對(duì)象的容器。例如,在 C++11 中,您可以使用 std::vector<std::unique_ptr<int>> 創(chuàng)建這樣的容器。但是,來自 Boost.PointerContainer 的容器可以提供一些額外的便利。
Example2.1.Usingboost::ptr_vector
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
int main()
{
boost::ptr_vector<int> v;
v.push_back(new int{1});
v.push_back(new int{2});
std::cout << v.back() << '\n';
}類 boost::ptr_vector 基本上像 std::vector<std::unique_ptr<int>> 一樣工作(參見示例 2.1)。但是,因?yàn)?boost::ptr_vector 知道它存儲(chǔ)動(dòng)態(tài)分配的對(duì)象,所以像 back() 這樣的成員函數(shù)會(huì)返回對(duì)動(dòng)態(tài)分配對(duì)象的引用,而不是指針。因此,該示例將 2 寫入標(biāo)準(zhǔn)輸出。
例子1.boost::ptr_set以直觀正確的順序
#include <boost/ptr_container/ptr_set.hpp>
#include <boost/ptr_container/indirect_fun.hpp>
#include <set>
#include <memory>
#include <functional>
#include <iostream>
int main()
{
boost::ptr_set<int> s;
s.insert(new int{2});
s.insert(new int{1});
std::cout << *s.begin() << '\n';
std::set<std::unique_ptr<int>, boost::indirect_fun<std::less<int>>> v;
v.insert(std::unique_ptr<int>(new int{2}));
v.insert(std::unique_ptr<int>(new int{1}));
std::cout << **v.begin() << '\n';
}示例 1 說明了使用專用容器的另一個(gè)原因。該示例將 int 類型的動(dòng)態(tài)分配變量存儲(chǔ)在 boost::ptr_set 和 std::set 中。 std::set 與 std::unique_ptr 一起使用。
使用 boost::ptr_set,元素的順序取決于 int 值。 std::set 比較 std::unique_ptr 類型的指針,而不是指針引用的變量。要使 std::set 根據(jù) int 值對(duì)元素進(jìn)行排序,必須告知容器如何比較元素。在示例 1 中,使用了 boost::indirect_fun(由 Boost.PointerContainer 提供)。使用 boost::indirect_fun,std::set 被告知不應(yīng)該根據(jù) std::unique_ptr 類型的指針對(duì)元素進(jìn)行排序,而是根據(jù)指針?biāo)傅?int 值。這就是示例顯示 1 兩次的原因。
除了 boost::ptr_vector 和 boost::ptr_set 之外,還有其他容器可用于管理動(dòng)態(tài)分配的對(duì)象。這些附加容器的示例包括 boost::ptr_deque、boost::ptr_list、boost::ptr_map、boost::ptr_unordered_set 和 boost::ptr_unordered_map。這些容器對(duì)應(yīng)于標(biāo)準(zhǔn)庫中眾所周知的容器。
例子2 .來自 Boost.PointerContainer 的容器插入器
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/ptr_container/ptr_inserter.hpp>
#include <array>
#include <algorithm>
#include <iostream>
int main()
{
boost::ptr_vector<int> v;
std::array<int, 3> a{{0, 1, 2}};
std::copy(a.begin(), a.end(), boost::ptr_container::ptr_back_inserter(v));
std::cout << v.size() << '\n';
}Boost.PointerContainer 為其容器提供插入器。它們在命名空間 boost::ptr_container 中定義。要訪問插入器,您必須包含頭文件 boost/ptr_container/ptr_inserter.hpp。
示例 2 使用函數(shù) boost::ptr_container::ptr_back_inserter(),它創(chuàng)建了一個(gè) boost::ptr_container::ptr_back_insert_iterator 類型的插入器。此插入器被傳遞給 std::copy() 以將數(shù)組 a 中的所有數(shù)字復(fù)制到向量 v。因?yàn)?v 是 boost::ptr_vector 類型的容器,它需要?jiǎng)討B(tài)分配的 int 對(duì)象的地址,所以插入器使用堆上的新地址并將地址添加到容器中。
除了 boost::ptr_container::ptr_back_inserter() 之外,Boost.PointerContainer 還提供了 boost::ptr_container::ptr_front_inserter() 和 boost::ptr_container::ptr_inserter() 函數(shù)來創(chuàng)建相應(yīng)的插入器。
三、練習(xí)
使用成員變量 name、legs 和 has_tail 創(chuàng)建一個(gè)包含多個(gè)動(dòng)物類型對(duì)象的程序。將對(duì)象存儲(chǔ)在 Boost.PointerContainer 的容器中。根據(jù)腿按升序?qū)θ萜鬟M(jìn)行排序,并將所有元素寫入標(biāo)準(zhǔn)輸出。
到此這篇關(guān)于C++ Boost PointerContainer智能指針詳解的文章就介紹到這了,更多相關(guān)C++ Boost PointerContainer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)掃雷游戲(控制臺(tái)不閃屏版)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)掃雷游戲,控制臺(tái)不閃屏版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
C語言 動(dòng)態(tài)內(nèi)存分配的詳解及實(shí)例
這篇文章主要介紹了C語言 動(dòng)態(tài)內(nèi)存分配的詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09
C語言浮點(diǎn)函數(shù)中的modf和fmod詳解
這篇文章主要為大家詳細(xì)介紹了C語言浮點(diǎn)函數(shù)中的modf和fmod,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
C語言算法練習(xí)之?dāng)?shù)組求素?cái)?shù)
這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組求素?cái)?shù)的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-09-09
C語言實(shí)現(xiàn)二叉樹的搜索及相關(guān)算法示例
這篇文章主要介紹了C語言實(shí)現(xiàn)二叉樹的搜索及相關(guān)算法,結(jié)合具體實(shí)例形式分析了基于C語言創(chuàng)建、遍歷、搜索等相關(guān)算法與實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
詳解Qt使用QImage類實(shí)現(xiàn)圖像基本操作
這篇文章主要介紹了Qt如何利用QImage類實(shí)現(xiàn)對(duì)圖像的基本操作,包括圖像顯示、圖像縮放、圖像旋轉(zhuǎn)等,感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-06-06

