C++ Boost PointerContainer智能指針詳解
一、提要
在 C++11 中,Boost.PointerContainer是另一個(gè)智能指針,一般是用來(lái)生成集合數(shù)據(jù)的,本文闡述這種指針的特點(diǎn)和用法。
二、智能指針Boost.PointerContainer
庫(kù) Boost.PointerContainer 提供專(zhuān)門(mén)用于管理動(dòng)態(tài)分配對(duì)象的容器。例如,在 C++11 中,您可以使用 std::vector<std::unique_ptr<int>> 創(chuàng)建這樣的容器。但是,來(lái)自 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'; }
類(lèi) boost::ptr_vector 基本上像 std::vector<std::unique_ptr<int>> 一樣工作(參見(jiàn)示例 2.1)。但是,因?yàn)?boost::ptr_vector 知道它存儲(chǔ)動(dòng)態(tài)分配的對(duì)象,所以像 back() 這樣的成員函數(shù)會(huì)返回對(duì)動(dòng)態(tài)分配對(duì)象的引用,而不是指針。因此,該示例將 2 寫(xiě)入標(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 說(shuō)明了使用專(zhuān)用容器的另一個(gè)原因。該示例將 int 類(lèi)型的動(dòng)態(tài)分配變量存儲(chǔ)在 boost::ptr_set 和 std::set 中。 std::set 與 std::unique_ptr 一起使用。
使用 boost::ptr_set,元素的順序取決于 int 值。 std::set 比較 std::unique_ptr 類(lèi)型的指針,而不是指針引用的變量。要使 std::set 根據(jù) int 值對(duì)元素進(jìn)行排序,必須告知容器如何比較元素。在示例 1 中,使用了 boost::indirect_fun(由 Boost.PointerContainer 提供)。使用 boost::indirect_fun,std::set 被告知不應(yīng)該根據(jù) std::unique_ptr 類(lèi)型的指針對(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)庫(kù)中眾所周知的容器。
例子2 .來(lái)自 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 為其容器提供插入器。它們?cè)诿臻g boost::ptr_container 中定義。要訪問(wèn)插入器,您必須包含頭文件 boost/ptr_container/ptr_inserter.hpp。
示例 2 使用函數(shù) boost::ptr_container::ptr_back_inserter(),它創(chuàng)建了一個(gè) boost::ptr_container::ptr_back_insert_iterator 類(lèi)型的插入器。此插入器被傳遞給 std::copy() 以將數(shù)組 a 中的所有數(shù)字復(fù)制到向量 v。因?yàn)?v 是 boost::ptr_vector 類(lèi)型的容器,它需要?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ù)來(lái)創(chuàng)建相應(yīng)的插入器。
三、練習(xí)
使用成員變量 name、legs 和 has_tail 創(chuàng)建一個(gè)包含多個(gè)動(dòng)物類(lèi)型對(duì)象的程序。將對(duì)象存儲(chǔ)在 Boost.PointerContainer 的容器中。根據(jù)腿按升序?qū)θ萜鬟M(jìn)行排序,并將所有元素寫(xiě)入標(biāo)準(zhǔn)輸出。
到此這篇關(guān)于C++ Boost PointerContainer智能指針詳解的文章就介紹到這了,更多相關(guān)C++ Boost PointerContainer內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT 中文亂碼解決匯總(QString與string、char*互轉(zhuǎn)亂碼)
在QT中使用中文時(shí),經(jīng)常會(huì)碰到論碼問(wèn)題,本文主要介紹了QT 中文亂碼解決匯總(QString與string、char*互轉(zhuǎn)亂碼),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07C++實(shí)現(xiàn)查找二叉樹(shù)中和為某一值的所有路徑的示例
這篇文章主要介紹了C++實(shí)現(xiàn)查找二叉樹(shù)中和為某一值的所有路徑的示例,文中的方法是根據(jù)數(shù)組生成二叉排序樹(shù)并進(jìn)行遍歷,需要的朋友可以參考下2016-02-02基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要介紹了基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn),主要操作是possibleMove(int?x,?int?y),通過(guò)整個(gè)棋盤(pán)每個(gè)位置上的信息、中國(guó)象棋的規(guī)則來(lái)獲得位置(x,?y)這個(gè)棋子可以移動(dòng)到的位置,需要的朋友可以參考一下2022-02-02C語(yǔ)言實(shí)例實(shí)現(xiàn)二叉搜索樹(shù)詳解
二叉搜索樹(shù)是以一棵二叉樹(shù)來(lái)組織的。每個(gè)節(jié)點(diǎn)是一個(gè)對(duì)象,包含的屬性有l(wèi)eft,right,p和key,其中,left指向該節(jié)點(diǎn)的左孩子,right指向該節(jié)點(diǎn)的右孩子,p指向該節(jié)點(diǎn)的父節(jié)點(diǎn),key是它的值2022-05-05C語(yǔ)言實(shí)現(xiàn)小小圣誕樹(shù)源代碼
圣誕節(jié)當(dāng)然要有個(gè)圣誕樹(shù)了,今天給你們用C語(yǔ)言編寫(xiě)一個(gè)雪夜圣誕樹(shù),這篇文章主要給大家介紹了關(guān)于C語(yǔ)言實(shí)現(xiàn)小小圣誕樹(shù)的相關(guān)資料,需要的朋友可以參考下2023-12-12C語(yǔ)言中實(shí)現(xiàn)“17進(jìn)制”轉(zhuǎn)“10進(jìn)制”實(shí)例代碼
這篇文章主要介紹了C語(yǔ)言中實(shí)現(xiàn)“17進(jìn)制”轉(zhuǎn)“10進(jìn)制”實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05