C++ Boost Flyweight庫使用介紹
一、說明
以下庫用于設(shè)計(jì)模式。
- Boost.Flyweight 有助于在程序中使用許多相同的對(duì)象并且需要減少內(nèi)存消耗的情況。
- Boost.Signals2 使得使用觀察者設(shè)計(jì)模式變得容易。這個(gè)庫被稱為 Boost.Signals2 因?yàn)樗鼘?shí)現(xiàn)了信號(hào)/槽的概念。
- Boost.MetaStateMachine 使得將狀態(tài)機(jī)從 UML 轉(zhuǎn)移到 C++ 成為可能。
本節(jié)內(nèi)容
二、庫Boost.Flyweight
Boost.Flyweight 是一個(gè)可以輕松使用同名設(shè)計(jì)模式的庫。當(dāng)許多對(duì)象共享數(shù)據(jù)時(shí),享元有助于節(jié)省內(nèi)存。使用這種設(shè)計(jì)模式,不是在對(duì)象中多次存儲(chǔ)相同的數(shù)據(jù),而是將共享數(shù)據(jù)保存在一個(gè)地方,所有對(duì)象都引用該數(shù)據(jù)。雖然您可以使用例如指針來實(shí)現(xiàn)此設(shè)計(jì)模式,但使用 Boost.Flyweight 更容易。
示例 66.1。沒有 Boost.Flyweight 的十萬個(gè)相同的字符串
#include <string>
#include <vector>
struct person
{
int id_;
std::string city_;
};
int main()
{
std::vector<person> persons;
for (int i = 0; i < 100000; ++i)
persons.push_back({i, "Berlin"});
}示例 66.1 創(chuàng)建了十萬個(gè) person 類型的對(duì)象。 person 定義了兩個(gè)成員變量:id_ 標(biāo)識(shí)人,city_ 存儲(chǔ)人們居住的城市。在這個(gè)例子中,所有人都住在柏林。這就是為什么 city_ 在所有十萬個(gè)對(duì)象中都設(shè)置為“Berlin”。因此,該示例使用十萬個(gè)字符串,所有字符串都設(shè)置為相同的值。使用 Boost.Flyweight,可以使用一個(gè)字符串——而不是數(shù)千個(gè)——并且可以減少內(nèi)存消耗。
示例 66.2。使用 Boost.Flyweight 一個(gè)字符串而不是十萬個(gè)字符串
#include <boost/flyweight.hpp>
#include <string>
#include <vector>
#include <utility>
using namespace boost::flyweights;
struct person
{
int id_;
flyweight<std::string> city_;
person(int id, std::string city) : id_{id}, city_{std::move(city)} {}
};
int main()
{
std::vector<person> persons;
for (int i = 0; i < 100000; ++i)
persons.push_back({i, "Berlin"});
}要使用 Boost.Flyweight,請(qǐng)包含 boost/flyweight.hpp,如示例 66.2 所示。 Boost.Flyweight 提供了額外的頭文件,僅當(dāng)您需要更改詳細(xì)的庫設(shè)置時(shí)才需要包含這些頭文件。
所有類和函數(shù)都在命名空間 boost::flyweights 中。示例 66.2 僅使用類 boost::flyweights::flyweight,這是該庫中最重要的類。成員變量 city_ 使用類型 flyweight<std::string> 而不是 std::string。這是您需要更改的所有內(nèi)容,以使用此設(shè)計(jì)模式并減少程序的內(nèi)存需求。
示例 66.3。多次使用 boost::flyweights::flyweight
#include <boost/flyweight.hpp>
#include <string>
#include <vector>
#include <utility>
using namespace boost::flyweights;
struct person
{
int id_;
flyweight<std::string> city_;
flyweight<std::string> country_;
person(int id, std::string city, std::string country)
: id_{id}, city_{std::move(city)}, country_{std::move(country)} {}
};
int main()
{
std::vector<person> persons;
for (int i = 0; i < 100000; ++i)
persons.push_back({i, "Berlin", "Germany"});
}示例 66.3 向類 person 添加了第二個(gè)成員變量 country_。這個(gè)成員變量包含人們居住的國(guó)家的名字。因?yàn)樵谶@個(gè)例子中,所有人都住在柏林,所以他們都住在同一個(gè)國(guó)家。這就是為什么在成員變量 country_ 的定義中也使用了 boost::flyweights::flyweight。
Boost.Flyweight 使用一個(gè)內(nèi)部容器來存儲(chǔ)對(duì)象。它確保不能有多個(gè)具有相同值的對(duì)象。默認(rèn)情況下,Boost.Flyweight 使用哈希容器,例如 std::unordered_set。對(duì)于不同的類型,使用不同的散列容器。與示例 66.3 一樣,成員變量 city_ 和 country_ 都是字符串;因此,只使用一個(gè)容器。在此示例中,這不是問題,因?yàn)槿萜鲀H存儲(chǔ)兩個(gè)字符串:“Berlin”和“Germany”。如果必須存儲(chǔ)許多不同的城市和國(guó)家,最好將城市存儲(chǔ)在一個(gè)容器中,將國(guó)家存儲(chǔ)在另一個(gè)容器中。
示例 66.4。多次使用 boost::flyweights::flyweight 標(biāo)簽
#include <boost/flyweight.hpp>
#include <string>
#include <vector>
#include <utility>
using namespace boost::flyweights;
struct city {};
struct country {};
struct person
{
int id_;
flyweight<std::string, tag<city>> city_;
flyweight<std::string, tag<country>> country_;
person(int id, std::string city, std::string country)
: id_{id}, city_{std::move(city)}, country_{std::move(country)} {}
};
int main()
{
std::vector<person> persons;
for (int i = 0; i < 100000; ++i)
persons.push_back({i, "Berlin", "Germany"});
}示例 66.4 將第二個(gè)模板參數(shù)傳遞給 boost::flyweights::flyweight。這是一個(gè)標(biāo)簽。標(biāo)簽是任意類型,僅用于區(qū)分 city_ 和 country_ 所基于的類型。示例 66.4 定義了兩個(gè)空結(jié)構(gòu)城市和國(guó)家,用作標(biāo)簽。但是,該示例可以改為使用 int、bool 或任何類型。
標(biāo)簽使 city_ 和 country_ 使用不同的類型?,F(xiàn)在 Boost.Flyweight 使用了兩個(gè)哈希容器——一個(gè)存儲(chǔ)城市,另一個(gè)存儲(chǔ)國(guó)家。
示例 66.5。 boost::flyweights::flyweight 的模板參數(shù)
#include <boost/flyweight.hpp>
#include <boost/flyweight/set_factory.hpp>
#include <boost/flyweight/no_locking.hpp>
#include <boost/flyweight/no_tracking.hpp>
#include <string>
#include <vector>
#include <utility>
using namespace boost::flyweights;
struct person
{
int id_;
flyweight<std::string, set_factory<>, no_locking, no_tracking> city_;
person(int id, std::string city) : id_{id}, city_{std::move(city)} {}
};
int main()
{
std::vector<person> persons;
for (int i = 0; i < 100000; ++i)
persons.push_back({i, "Berlin"});
}標(biāo)簽以外的模板參數(shù)可以傳遞給 boost::flyweights::flyweight。示例 66.5 通過 boost::flyweights::set_factory、boost::flyweights::no_locking 和 boost::flyweights::no_tracking。包含額外的頭文件以使用這些類。
boost::flyweights::set_factory 告訴 Boost.Flyweight 使用排序容器,例如 std::set,而不是散列容器。使用 boost::flyweights::no_locking,通常默認(rèn)激活的對(duì)多線程的支持被停用。 boost::flyweights::no_tracking 告訴 Boost.Flyweight 不要跟蹤存儲(chǔ)在內(nèi)部容器中的對(duì)象。默認(rèn)情況下,當(dāng)不再使用對(duì)象時(shí),Boost.Flyweight 會(huì)檢測(cè)到這一點(diǎn)并將它們從容器中移除。當(dāng)設(shè)置了 boost::flyweights::no_tracking 時(shí),檢測(cè)機(jī)制被禁用。這提高了性能。但是,容器只能增長(zhǎng),永遠(yuǎn)不會(huì)收縮。
Boost.Flyweight 支持額外的設(shè)置。如果您對(duì)調(diào)整的更多細(xì)節(jié)感興趣,請(qǐng)查看官方文檔。
煉習(xí)
使用 Boost.Flyweight 改進(jìn)這個(gè)程序。使用禁用多線程支持的 Boost.Flyweight:
#include <string>
#include <vector>
#include <memory>
int main()
{
std::vector<std::shared_ptr<std::string>> countries;
auto germany = std::make_shared<std::string>("Germany");
for (int i = 0; i < 500; ++i)
countries.push_back(germany);
auto netherlands = std::make_shared<std::string>("Netherlands");
for (int i = 0; i < 500; ++i)
countries.push_back(netherlands);
}到此這篇關(guān)于C++ Boost Flyweight庫使用介紹的文章就介紹到這了,更多相關(guān)C++ Boost Flyweight內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放
ffplay是FFmpeg提供的一個(gè)極為簡(jiǎn)單的音視頻媒體播放器,可以用于音視頻播放、可視化分析。本文將利用ffplay實(shí)現(xiàn)自定義輸入流播放,需要的可以參考一下2022-12-12
Matlab計(jì)算變異函數(shù)并繪制經(jīng)驗(yàn)半方差圖詳解
這篇文章主要為大家詳細(xì)介紹了基于MATLAB求取空間數(shù)據(jù)的變異函數(shù),并繪制經(jīng)驗(yàn)半方差圖的方法。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-04-04
C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
OpenCV基于背景減除實(shí)現(xiàn)行人計(jì)數(shù)
本文主要介紹了如何使用OpenCV C++對(duì)視頻中的人流量進(jìn)行統(tǒng)計(jì)。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,需要的可以了解一下2022-01-01

