" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ Boost Bimap示例詳細(xì)講解

 更新時(shí)間:2022年11月05日 17:18:41   作者:無水先生  
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱

一、提要

庫 Boost.Bimap 基于 Boost.MultiIndex 并提供了一個(gè)無需先定義即可立即使用的容器。該容器類似于 std::map,但支持從任一側(cè)查找值。 Boost.Bimap 允許您根據(jù)訪問地圖的方式創(chuàng)建任意一側(cè)都可以作為關(guān)鍵點(diǎn)的地圖。當(dāng)您訪問左側(cè)作為鍵時(shí),右側(cè)是值,反之亦然。

二、示例

Example13.1.Usingboost::bimap

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string, int> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  std::cout << animals.left.count("cat") << '\n';
  std::cout << animals.right.count(8) << '\n';
}

boost::bimap 定義在 boost/bimap.hpp 中,提供了兩個(gè)成員變量 left 和 right ,可用于訪問 boost::bimap 統(tǒng)一的兩個(gè) std::map 類型的容器。在示例 13.1 中,left 使用 std::string 類型的鍵來訪問容器,而 right 使用 int 類型的鍵。

除了支持使用左側(cè)或右側(cè)容器訪問單個(gè)記錄外,boost::bimap 還允許您將記錄視為關(guān)系(參見示例 13.2)。

示例 13.2。訪問關(guān)系

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string, int> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  for (auto it = animals.begin(); it != animals.end(); ++it)
    std::cout << it->left << " has " << it->right << " legs\n";
}

不必使用左或右訪問記錄。通過迭代記錄,單個(gè)記錄的左右部分可通過迭代器獲得。

雖然 std::map 附帶一個(gè)名為 std::multimap 的容器,它可以使用相同的鍵存儲(chǔ)多條記錄,但 boost::bimap 沒有這樣的等價(jià)物。但是,這并不意味著在 boost::bimap 類型的容器中存儲(chǔ)具有相同鍵的多條記錄是不可能的。嚴(yán)格來說,兩個(gè)必需的模板參數(shù)指定左和右的容器類型,而不是要存儲(chǔ)的元素的類型。如果未指定容器類型,則默認(rèn)使用容器類型 boost::bimaps::set_of。此容器與 std::map 一樣,僅接受具有唯一鍵的記錄。

示例 13.3。顯式使用 boost::bimaps::set_of

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<boost::bimaps::set_of<std::string>,
    boost::bimaps::set_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  std::cout << animals.left.count("spider") << '\n';
  std::cout << animals.right.count(8) << '\n';
}

示例 13.3 指定了 boost::bimaps::set_of。

除了 boost::bimaps::set_of 之外的其他容器類型可用于自定義 boost::bimap。

示例 13.4。使用 boost::bimaps::multiset_of 允許重復(fù)

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<boost::bimaps::set_of<std::string>,
    boost::bimaps::multiset_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"dog", 4});
  std::cout << animals.left.count("dog") << '\n';
  std::cout << animals.right.count(4) << '\n';
}

Example13.4

示例 13.4 使用容器類型 boost::bimaps::multiset_of,它在 boost/bimap/multiset_of.hpp 中定義。它的工作方式類似于 boost::bimaps::set_of,除了鍵不需要是唯一的。示例 13.4 將在搜索有四條腿的動(dòng)物時(shí)成功顯示 2。

因?yàn)?boost::bimaps::set_of 默認(rèn)用于 boost::bimap 類型的容器,所以不需要顯式包含頭文件 boost/bimap/set_of.hpp。但是,當(dāng)使用其他容器類型時(shí),必須包含相應(yīng)的頭文件。

除了上面顯示的類之外,Boost.Bimap 還提供以下類:boost::bimaps::unordered_set_of、boost::bimaps::unordered_multiset_of、boost::bimaps::list_of、boost::bimaps::vector_of 和 boost: :bimaps::unconstrained_set_of。除了 boost::bimaps::unconstrained_set_of,所有其他容器類型都像標(biāo)準(zhǔn)庫中的對應(yīng)容器一樣運(yùn)行。

示例 13.5。使用 boost::bimaps::unconstrained_set_of 禁用一側(cè)

#include <boost/bimap.hpp>
#include <boost/bimap/unconstrained_set_of.hpp>
#include <boost/bimap/support/lambda.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string,
    boost::bimaps::unconstrained_set_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  auto it = animals.left.find("cat");
  animals.left.modify_key(it, boost::bimaps::_key = "dog");
  std::cout << it->first << '\n';
}

boost::bimaps::unconstrained_set_of 可用于禁用 boost::bimap 的一側(cè)。在示例 13.5 中,boost::bimap 的行為類似于 std::map。您無法訪問通過腿搜索動(dòng)物的權(quán)利。

示例 13.5 說明了為什么首選 boost::bimap 而不是 std::map 的另一個(gè)原因。由于 Boost.Bimap 基于 Boost.MultiIndex,因此 Boost.MultiIndex 的成員函數(shù)可用。示例 13.5 使用 modify_key() 修改密鑰——這是 std::map 無法實(shí)現(xiàn)的。

注意密鑰是如何修改的。使用 boost::bimaps::_key 為當(dāng)前鍵分配一個(gè)新值,這是一個(gè)在 boost/bimap/support/lambda.hpp 中定義的占位符。

boost/bimap/support/lambda.hpp 還定義了 boost::bimaps::_data。調(diào)用成員函數(shù) modify_data() 時(shí),boost::bimaps::_data 可用于修改 boost::bimap 類型容器中的值。

練習(xí)

使用 Boost.Bimap 實(shí)現(xiàn)類animals_container:

#include <boost/optional.hpp>
#include <string>
#include <vector>
#include <iostream>
struct animal
{
    std::string name;
    int legs;
    animal(std::string n, int l) : name(n), legs(l) {}
};
class animals_container
{
public:
    void add(animal a)
    {
        // TODO: Implement this member function.
    }
    boost::optional<animal> find_by_name(const std::string &name) const
    {
        // TODO: Implement this member function.
        return {};
    }
    std::vector<animal> find_by_legs(int from, int to) const
    {
        // TODO: Implement this member function.
        return {};
    }
};
int main()
{
    animals_container animals;
    animals.add({ "cat", 4 });
    animals.add({ "ant", 6 });
    animals.add({ "spider", 8 });
    animals.add({ "shark", 0 });
    auto shark = animals.find_by_name("shark");
    if (shark)
        std::cout << "shark has " << shark->legs << " legs\n";
    auto animals_with_4_to_6_legs = animals.find_by_legs(4, 7);
    for (auto animal : animals_with_4_to_6_legs)
        std::cout << animal.name << " has " << animal.legs << " legs\n";
}

到此這篇關(guān)于C++ Boost Bimap示例詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++ Boost Bimap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解C++編程中向函數(shù)傳遞引用參數(shù)的用法

    詳解C++編程中向函數(shù)傳遞引用參數(shù)的用法

    這篇文章主要介紹了詳解C++編程中向函數(shù)傳遞引用參數(shù)的用法,包括使函數(shù)返回引用類型以及對指針的引用,需要的朋友可以參考下
    2016-01-01
  • C/C++實(shí)現(xiàn)對STORM運(yùn)行信息查看及控制的方法

    C/C++實(shí)現(xiàn)對STORM運(yùn)行信息查看及控制的方法

    這篇文章主要介紹了C/C++實(shí)現(xiàn)對STORM運(yùn)行信息查看及控制的方法,需要的朋友可以參考下
    2014-07-07
  • C++中返回指向函數(shù)的指針示例

    C++中返回指向函數(shù)的指針示例

    int (*ff(int)) (int *,int);表示:ff(int)是一個(gè)函數(shù),帶有一個(gè)int型的形參,該函數(shù)返回int (*) (int *,int),它是一個(gè)指向函數(shù)的指針,所指向的函數(shù)返回int型并帶有兩個(gè)分別是Int*和int型的形參
    2013-09-09
  • 概率的問題:使用遞歸與多次試驗(yàn)?zāi)M的分析

    概率的問題:使用遞歸與多次試驗(yàn)?zāi)M的分析

    以下對概率的問題:使用了遞歸和多次試驗(yàn)?zāi)M。需要的朋友參考下
    2013-05-05
  • c++用指針交換數(shù)組的實(shí)例講解

    c++用指針交換數(shù)組的實(shí)例講解

    下面小編就為大家分享一篇c++用指針交換數(shù)組的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 實(shí)現(xiàn)一個(gè)random?shuffle算法示例

    實(shí)現(xiàn)一個(gè)random?shuffle算法示例

    這篇文章主要為大家介紹了實(shí)現(xiàn)一個(gè)random?shuffle算法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • C++ ostream用法案例詳解

    C++ ostream用法案例詳解

    這篇文章主要介紹了C++ ostream用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • VS2022創(chuàng)建Windows服務(wù)程序的方法步驟

    VS2022創(chuàng)建Windows服務(wù)程序的方法步驟

    本文主要介紹了VS2022創(chuàng)建Windows服務(wù)程序的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C語言代碼詳細(xì)描述順序線性表

    C語言代碼詳細(xì)描述順序線性表

    這篇文章主要用代碼介紹了C語言線性表的順序線性表,對于學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法的朋友很有參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • C語言之初識指針

    C語言之初識指針

    在C語言中,指針是一種保存變量地址的變量。這篇文章介紹了初識C語言指針,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12

最新評論