C++ Boost PointerContainer智能指針詳解
一、提要
在 C++11 中,Boost.PointerContainer是另一個智能指針,一般是用來生成集合數(shù)據(jù)的,本文闡述這種指針的特點和用法。
二、智能指針Boost.PointerContainer
庫 Boost.PointerContainer 提供專門用于管理動態(tài)分配對象的容器。例如,在 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)。但是,因為 boost::ptr_vector 知道它存儲動態(tài)分配的對象,所以像 back() 這樣的成員函數(shù)會返回對動態(tài)分配對象的引用,而不是指針。因此,該示例將 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 說明了使用專用容器的另一個原因。該示例將 int 類型的動態(tài)分配變量存儲在 boost::ptr_set 和 std::set 中。 std::set 與 std::unique_ptr 一起使用。
使用 boost::ptr_set,元素的順序取決于 int 值。 std::set 比較 std::unique_ptr 類型的指針,而不是指針引用的變量。要使 std::set 根據(jù) int 值對元素進(jìn)行排序,必須告知容器如何比較元素。在示例 1 中,使用了 boost::indirect_fun(由 Boost.PointerContainer 提供)。使用 boost::indirect_fun,std::set 被告知不應(yīng)該根據(jù) std::unique_ptr 類型的指針對元素進(jìn)行排序,而是根據(jù)指針?biāo)傅?int 值。這就是示例顯示 1 兩次的原因。
除了 boost::ptr_vector 和 boost::ptr_set 之外,還有其他容器可用于管理動態(tài)分配的對象。這些附加容器的示例包括 boost::ptr_deque、boost::ptr_list、boost::ptr_map、boost::ptr_unordered_set 和 boost::ptr_unordered_map。這些容器對應(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)建了一個 boost::ptr_container::ptr_back_insert_iterator 類型的插入器。此插入器被傳遞給 std::copy() 以將數(shù)組 a 中的所有數(shù)字復(fù)制到向量 v。因為 v 是 boost::ptr_vector 類型的容器,它需要動態(tài)分配的 int 對象的地址,所以插入器使用堆上的新地址并將地址添加到容器中。
除了 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)建一個包含多個動物類型對象的程序。將對象存儲在 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語言算法練習(xí)之?dāng)?shù)組求素數(shù)
這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組求素數(shù)的實現(xiàn)方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-09-09
C語言實現(xiàn)二叉樹的搜索及相關(guān)算法示例
這篇文章主要介紹了C語言實現(xiàn)二叉樹的搜索及相關(guān)算法,結(jié)合具體實例形式分析了基于C語言創(chuàng)建、遍歷、搜索等相關(guān)算法與實現(xiàn)技巧,需要的朋友可以參考下2017-06-06

