C++ Boost Container庫示例詳細(xì)講解
一、關(guān)于Boost.Container
Boost.Container 是一個(gè) Boost 庫,提供與標(biāo)準(zhǔn)庫相同的容器。 Boost.Container 專注于額外的靈活性。例如,這個(gè)庫中的所有容器都可以在共享內(nèi)存中與 Boost.Interprocess 一起使用——這對(duì)于標(biāo)準(zhǔn)庫中的容器并不總是可行的。
Boost.Container 提供了額外的優(yōu)勢(shì):
- 容器的接口類似于 C++11 標(biāo)準(zhǔn)庫中容器的接口。例如,它們提供諸如 emplace_back() 之類的成員函數(shù),您可以在 C++98 程序中使用它,即使它直到 C++11 才被添加到標(biāo)準(zhǔn)庫中。
- 借助 boost::container::slist 或 boost::container::stable_vector,Boost.Container 提供了標(biāo)準(zhǔn)庫不提供的容器。
- 該實(shí)現(xiàn)與平臺(tái)無關(guān)。容器在任何地方的行為都相同。您無需擔(dān)心標(biāo)準(zhǔn)庫實(shí)現(xiàn)之間可能存在的差異。
- Boost.Container 中的容器支持不完整的類型,可用于定義遞歸容器。
二、Boost.Container示例
示例 20.1 說明了不完整的類型。
注意
本章中的示例無法使用 Visual C++ 2013 和 Boost 1.55.0 編譯。此錯(cuò)誤在工單 9332 中進(jìn)行了描述。它已在 Boost 1.56.0 中修復(fù)。
示例 20.1。帶有 Boost.Container 的遞歸容器
#include <boost/container/vector.hpp>
using namespace boost::container;
struct animal
{
vector<animal> children;
};
int main()
{
animal parent, child1, child2;
parent.children.push_back(child1);
parent.children.push_back(child2);
}類動(dòng)物有一個(gè)類型為 boost::container::vector<animal> 的成員變量 children。 boost::container::vector 在頭文件 boost/container/vector.hpp 中定義。因此,成員變量children 的類型基于定義變量children 的類animal。在這一點(diǎn)上,還沒有完全定義動(dòng)物。雖然該標(biāo)準(zhǔn)不要求標(biāo)準(zhǔn)庫中的容器支持不完整類型,但 Boost.Container 明確支持遞歸容器。標(biāo)準(zhǔn)庫定義的容器是否可以遞歸使用取決于實(shí)現(xiàn)。
示例 20.2。使用 boost::container::stable_vector
#include <boost/container/stable_vector.hpp>
#include <iostream>
using namespace boost::container;
int main()
{
stable_vector<int> v(2, 1);
int &i = v[1];
v.erase(v.begin());
std::cout << i << '\n';
}除了標(biāo)準(zhǔn)庫中眾所周知的容器之外,Boost.Container 還提供容器。示例 20.2 引入了容器 boost::container::stable_vector,其行為類似于 std::vector,除了如果 boost::container::stable_vector 更改,所有迭代器和對(duì)現(xiàn)有元素的引用仍然有效。這是可能的,因?yàn)樵貨]有連續(xù)存儲(chǔ)在 boost::container::stable_vector 中。即使元素沒有彼此相鄰存儲(chǔ)在內(nèi)存中,仍然可以使用索引訪問元素。
Boost.Container 保證示例 20.2 中的引用 i 在向量中的第一個(gè)元素被擦除時(shí)仍然有效。該示例顯示 1。
請(qǐng)注意,boost::container::stable_vector 和該庫中的其他容器都不支持 C++11 初始化列表。在示例 20.2 中,v 被初始化為兩個(gè)元素都設(shè)置為 1。
boost::container::stable_vector 在 boost/container/stable_vector.hpp 中定義。
Boost.Container 提供的其他容器是 boost::container::flat_set、boost::container::flat_map、boost::container::slist 和 boost::container::static_vector:
- boost::container::flat_set 和 boost::container::flat_map 類似于 std::set 和 std::map。然而,它們被實(shí)現(xiàn)為排序向量,而不是樹。這允許更快的查找和迭代,但插入和刪除元素的成本更高。
- 這兩個(gè)容器在頭文件 boost/container/flat_set.hpp 和 boost/container/flat_map.hpp 中定義。
- boost::container::slist 是一個(gè)單鏈表。它類似于使用 C++11 添加到標(biāo)準(zhǔn)庫的 std::forward_list。
- boost::container::slist 提供了一個(gè)成員函數(shù) size(),它在 std::forward_list 中沒有。
- boost::container::slist 在 boost/container/slist.hpp 中定義。
- boost::container::static_vector 將 std::array 等元素??直接存儲(chǔ)在容器中。與 std::array 一樣,容器具有恒定的容量,盡管容量并沒有說明元素的數(shù)量。成員函數(shù) push_back()、pop_back()、insert() 和erase() 可用于插入或刪除元素。在這方面, boost::container::static_vector 類似于 std::vector。成員函數(shù) size() 返回容器中當(dāng)前存儲(chǔ)元素的數(shù)量。
- 容量是恒定的,但可以使用 resize() 更改。 push_back() 不會(huì)改變?nèi)萘?。僅當(dāng)容量大于當(dāng)前存儲(chǔ)的元素?cái)?shù)量時(shí),您才可以使用 push_back() 添加元素。否則,push_back() 會(huì)拋出 std::bad_alloc 類型的異常。
boost::container::static_vector 在 boost/container/static_vector.hpp 中定義。
到此這篇關(guān)于C++ Boost Container庫示例詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++ Boost Container內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言pow()函數(shù)實(shí)現(xiàn)求x的y次方的值
這篇文章主要介紹了C語言pow()函數(shù)實(shí)現(xiàn)求x的y次方的值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
這篇文章主要介紹了C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09

