C++ Boost Container庫示例詳細講解
一、關(guān)于Boost.Container
Boost.Container 是一個 Boost 庫,提供與標準庫相同的容器。 Boost.Container 專注于額外的靈活性。例如,這個庫中的所有容器都可以在共享內(nèi)存中與 Boost.Interprocess 一起使用——這對于標準庫中的容器并不總是可行的。
Boost.Container 提供了額外的優(yōu)勢:
- 容器的接口類似于 C++11 標準庫中容器的接口。例如,它們提供諸如 emplace_back() 之類的成員函數(shù),您可以在 C++98 程序中使用它,即使它直到 C++11 才被添加到標準庫中。
- 借助 boost::container::slist 或 boost::container::stable_vector,Boost.Container 提供了標準庫不提供的容器。
- 該實現(xiàn)與平臺無關(guān)。容器在任何地方的行為都相同。您無需擔心標準庫實現(xiàn)之間可能存在的差異。
- Boost.Container 中的容器支持不完整的類型,可用于定義遞歸容器。
二、Boost.Container示例
示例 20.1 說明了不完整的類型。
注意
本章中的示例無法使用 Visual C++ 2013 和 Boost 1.55.0 編譯。此錯誤在工單 9332 中進行了描述。它已在 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); }
類動物有一個類型為 boost::container::vector<animal> 的成員變量 children。 boost::container::vector 在頭文件 boost/container/vector.hpp 中定義。因此,成員變量children 的類型基于定義變量children 的類animal。在這一點上,還沒有完全定義動物。雖然該標準不要求標準庫中的容器支持不完整類型,但 Boost.Container 明確支持遞歸容器。標準庫定義的容器是否可以遞歸使用取決于實現(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'; }
除了標準庫中眾所周知的容器之外,Boost.Container 還提供容器。示例 20.2 引入了容器 boost::container::stable_vector,其行為類似于 std::vector,除了如果 boost::container::stable_vector 更改,所有迭代器和對現(xiàn)有元素的引用仍然有效。這是可能的,因為元素沒有連續(xù)存儲在 boost::container::stable_vector 中。即使元素沒有彼此相鄰存儲在內(nèi)存中,仍然可以使用索引訪問元素。
Boost.Container 保證示例 20.2 中的引用 i 在向量中的第一個元素被擦除時仍然有效。該示例顯示 1。
請注意,boost::container::stable_vector 和該庫中的其他容器都不支持 C++11 初始化列表。在示例 20.2 中,v 被初始化為兩個元素都設(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。然而,它們被實現(xiàn)為排序向量,而不是樹。這允許更快的查找和迭代,但插入和刪除元素的成本更高。
- 這兩個容器在頭文件 boost/container/flat_set.hpp 和 boost/container/flat_map.hpp 中定義。
- boost::container::slist 是一個單鏈表。它類似于使用 C++11 添加到標準庫的 std::forward_list。
- boost::container::slist 提供了一個成員函數(shù) size(),它在 std::forward_list 中沒有。
- boost::container::slist 在 boost/container/slist.hpp 中定義。
- boost::container::static_vector 將 std::array 等元素??直接存儲在容器中。與 std::array 一樣,容器具有恒定的容量,盡管容量并沒有說明元素的數(shù)量。成員函數(shù) push_back()、pop_back()、insert() 和erase() 可用于插入或刪除元素。在這方面, boost::container::static_vector 類似于 std::vector。成員函數(shù) size() 返回容器中當前存儲元素的數(shù)量。
- 容量是恒定的,但可以使用 resize() 更改。 push_back() 不會改變?nèi)萘俊H當容量大于當前存儲的元素數(shù)量時,您才可以使用 push_back() 添加元素。否則,push_back() 會拋出 std::bad_alloc 類型的異常。
boost::container::static_vector 在 boost/container/static_vector.hpp 中定義。
到此這篇關(guān)于C++ Boost Container庫示例詳細講解的文章就介紹到這了,更多相關(guān)C++ Boost Container內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++動態(tài)內(nèi)存分配(new/new[]和delete/delete[])詳解
這篇文章主要介紹了C++動態(tài)內(nèi)存分配(new/new[]和delete/delete[])詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05