C++ Boost Algorithm算法超詳細(xì)精講
一、說明Boost.Algorithm
請注意,其他 Boost 庫提供了許多算法。例如,您會在 Boost.StringAlgorithms 中找到處理字符串的算法。 Boost.Algorithm 提供的算法不受特定類的約束,例如 std::string。與標(biāo)準(zhǔn)庫中的算法一樣,它們可以與任何容器一起使用。
二、示例
示例 29.1。使用 boost::algorithm::one_of_equal() 測試一個值
#include <boost/algorithm/cxx11/one_of.hpp>
#include <array>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::array<int, 6> a{{0, 5, 2, 1, 4, 3}};
auto predicate = [](int i){ return i == 4; };
std::cout.setf(std::ios::boolalpha);
std::cout << one_of(a.begin(), a.end(), predicate) << '\n';
std::cout << one_of_equal(a.begin(), a.end(), 4) << '\n';
}boost::algorithm::one_of() 測試一個條件是否只滿足一次。要測試的條件作為謂詞傳遞。在示例 29.1 中,對 boost::algorithm::one_of() 的調(diào)用返回 true,因?yàn)閿?shù)字 4 在 a 中僅存儲一次。
要測試容器中的元素是否相等,請調(diào)用 boost::algorithm::one_of_equal()。你沒有傳遞謂詞。相反,您傳遞一個值以與 boost::algorithm::one_of_equal() 進(jìn)行比較。在示例 29.1 中,對 boost::algorithm::one_of_equal() 的調(diào)用也返回 true。
boost::algorithm::one_of() 是對 std::all_of()、std::any_of() 和 std::none_of() 算法的補(bǔ)充,這些算法是使用 C++11 添加到標(biāo)準(zhǔn)庫中的。但是,Boost.Algorithm 為開發(fā)環(huán)境不支持 C++ 的開發(fā)人員提供了函數(shù) boost::algorithm::all_of()、boost::algorithm::any_of() 和 boost::algorithm::none_of() 11.您可以在頭文件 boost/algorithm/cxx11/all_of.hpp、boost/algorithm/cxx11/any_of.hpp 和 boost/algorithm/cxx11/none_of.hpp 中找到這些算法。
Boost.Algorithm 還定義了以下函數(shù):boost::algorithm::all_of_equal()、boost::algorithm::any_of_equal() 和 boost::algorithm::none_of_equal()。
Boost.Algorithm 提供了更多來自 C++11 標(biāo)準(zhǔn)庫的算法。例如,您可以訪問 boost::algorithm::is_partitioned()、boost::algorithm::is_permutation()、boost::algorithm::copy_n()、boost::algorithm::find_if_not() 和 boost::算法::iota()。這些函數(shù)的工作方式與 C++11 標(biāo)準(zhǔn)庫中的同名函數(shù)類似,并且是為不使用 C++11 的開發(fā)人員提供的。但是,Boost.Algorithm 提供了一些對 C++11 開發(fā)人員也很有用的函數(shù)變體。
示例 29.2。 C++11 算法的更多變體
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx11/is_sorted.hpp>
#include <boost/algorithm/cxx11/copy_if.hpp>
#include <vector>
#include <iterator>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::vector<int> v;
iota_n(std::back_inserter(v), 10, 5);
std::cout.setf(std::ios::boolalpha);
std::cout << is_increasing(v) << '\n';
std::ostream_iterator<int> out{std::cout, ","};
copy_until(v, out, [](int i){ return i > 12; });
}Boost.Algorithm 在頭文件 boost/algorithm/cxx11/iota.hpp 中提供了 C++11 算法 boost::algorithm::iota()。此函數(shù)生成順序遞增的數(shù)字。它需要兩個迭代器用于容器的開頭和結(jié)尾。然后容器中的元素會被順序增加的數(shù)字覆蓋。
示例 29.2 使用 boost::algorithm::iota_n() 代替 boost::algorithm::iota()。此函數(shù)需要一個迭代器將數(shù)字寫入。要生成的數(shù)字?jǐn)?shù)量作為第三個參數(shù)傳遞給 boost::algorithm::iota_n()。
boost::algorithm::is_increasing() 和 boost::algorithm::is_sorted() 在頭文件 boost/algorithm/cxx11/is_sorted.hpp 中定義。 boost::algorithm::is_increasing() 與 boost::algorithm::is_sorted() 具有相同的功能,但函數(shù)名稱更清楚地表示該函數(shù)檢查值是否按升序排列。頭文件還定義了相關(guān)的函數(shù) boost::algorithm::is_decreasing()。
在示例 29.2 中,v 直接傳遞給 boost::algorithm::is_increasing()。 Boost.Algorithm 提供的所有函數(shù)都有一個基于范圍操作的變體。容器可以直接傳遞給這些函數(shù)。
boost::algorithm::copy_until() 在 boost/algorithm/cxx11/copy_if.hpp 中定義。這是 std::copy() 的另一個變體。 Boost.Algorithm 還提供了 boost::algorithm::copy_while()。
示例 29.2 作為 boost::algorithm::is_increasing() 的結(jié)果顯示為 true,并且 boost::algorithm::copy_until() 將數(shù)字 10、11 和 12 寫入標(biāo)準(zhǔn)輸出。
示例 29.3。來自 Boost.Algorithm 的 C++14 算法
#include <boost/algorithm/cxx14/equal.hpp>
#include <boost/algorithm/cxx14/mismatch.hpp>
#include <vector>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::vector<int> v{1, 2};
std::vector<int> w{1, 2, 3};
std::cout.setf(std::ios::boolalpha);
std::cout << equal(v.begin(), v.end(), w.begin(), w.end()) << '\n';
auto pair = mismatch(v.begin(), v.end(), w.begin(), w.end());
if (pair.first != v.end())
std::cout << *pair.first << '\n';
if (pair.second != w.end())
std::cout << *pair.second << '\n';
}除了來自 C++11 標(biāo)準(zhǔn)庫的算法,Boost.Algorithm 還定義了很可能會添加到 C++14 標(biāo)準(zhǔn)庫中的算法。示例 29.3 使用了其中兩個函數(shù)的新變體,boost::algorithm::equal() 和 boost::algorithm::mismatch()。與自 C++98 以來已成為標(biāo)準(zhǔn)庫一部分的同名函數(shù)相比,將四個迭代器(而不是三個)傳遞給這些新函數(shù)。示例 29.3 中的算法不期望第二個序列包含與第一個序列一樣多的元素。
boost::algorithm::equal() 返回一個 bool,boost::algorithm::mismatch() 返回一個 std::pair 中的兩個迭代器。第一個和第二個是指第一個和第二個序列中第一個不匹配的元素。這些迭代器也可以引用序列的結(jié)尾。
示例 29.3 將 false 和 3 寫入標(biāo)準(zhǔn)輸出。 false 是 boost::algorithm::equal() 的返回值,3 w 中的第三個元素。因?yàn)?v 和 w 中的前兩個元素相等,所以 boost::algorithm::mismatch() 首先返回到 v 末尾的迭代器,然后返回到 w 的第三個元素的迭代器。因?yàn)?first 指的是 v 的結(jié)尾,所以迭代器沒有被取消引用,也沒有輸出。
示例 29.4。使用 boost::algorithm::hex() 和 boost::algorithm::unhex()
#include <boost/algorithm/hex.hpp>
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::vector<char> v{'C', '+', '+'};
hex(v, std::ostream_iterator<char>{std::cout, ""});
std::cout << '\n';
std::string s = "C++";
std::cout << hex(s) << '\n';
std::vector<char> w{'4', '3', '2', 'b', '2', 'b'};
unhex(w, std::ostream_iterator<char>{std::cout, ""});
std::cout << '\n';
std::string t = "432b2b";
std::cout << unhex(t) << '\n';
}示例 29.4 使用了兩個函數(shù) boost::algorithm::hex() 和 boost::algorithm::unhex()。這些函數(shù)是根據(jù)數(shù)據(jù)庫系統(tǒng) MySQL 中的同名函數(shù)設(shè)計的。它們將字符轉(zhuǎn)換為十六進(jìn)制值或?qū)⑹M(jìn)制值轉(zhuǎn)換為字符。
示例 29.4 將帶有字符“C”、“+”和“+”的向量 v 傳遞給 boost::algorithm::hex()。此函數(shù)需要一個迭代器作為第二個參數(shù)來寫入十六進(jìn)制值。該示例將“C”的 43 和“+”的兩個實(shí)例的 2B(兩次)寫入標(biāo)準(zhǔn)輸出。對 boost::algorithm::hex() 的第二次調(diào)用執(zhí)行相同的操作,只是“C++”作為字符串傳遞,而“432B2B”作為字符串返回。
boost::algorithm::unhex() 與 boost::algorithm::hex() 相反。如果示例 29.4 中的數(shù)組 w 使用六個十六進(jìn)制值傳遞,則三對值中的每一對都被解釋為 ASCII 碼。當(dāng)六個十六進(jìn)制值作為字符串傳遞時,第二次調(diào)用 boost::algorithm::unhex() 也會發(fā)生同樣的情況。在這兩種情況下,C++ 都被寫入標(biāo)準(zhǔn)輸出。
Boost.Algorithm 提供了更多的算法。例如,有幾種字符串匹配算法可以有效地搜索文本。該文檔包含所有可用算法的概述。
練習(xí)
使用 Boost.Algorithm 中的函數(shù)以升序?qū)?shù)字 51 到 56 分配給具有六個元素的數(shù)組。將數(shù)組中的數(shù)字解釋為十六進(jìn)制值,將它們轉(zhuǎn)換為字符并將結(jié)果寫入標(biāo)準(zhǔn)輸出。
到此這篇關(guān)于C++ Boost Algorithm算法超詳細(xì)精講的文章就介紹到這了,更多相關(guān)C++ Boost Algorithm內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt數(shù)據(jù)庫應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)分組導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)分組導(dǎo)出,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價值,需要的可以了解一下2022-06-06
C++中String的語法及常用接口的底層實(shí)現(xiàn)詳解
在C語言中,string是一個標(biāo)準(zhǔn)庫類(class),用于處理字符串,它提供了一種更高級、更便捷的字符串操作方式,string 類提供了一系列成員函數(shù)和重載運(yùn)算符,以便于對字符串進(jìn)行操作和處理,本編文章會對C++中的 string 進(jìn)行詳解,希望本篇文章會對你有所幫助2023-06-06
C++生成隨機(jī)浮點(diǎn)數(shù)的示例代碼
在C++11之前,我們通常采用rand函數(shù)來生成隨機(jī)數(shù),但rand函數(shù)對一些情況顯得難以處理。本文將介紹如何利用C++生成隨機(jī)浮點(diǎn)數(shù),需要的可以參考一下2022-04-04
Qt圖形圖像開發(fā)之曲線圖表庫QChart編譯安裝詳細(xì)方法與使用實(shí)例
這篇文章主要介紹了Qt圖形圖像開發(fā)之曲線圖表庫QChart編譯安裝詳細(xì)方法與使用實(shí)例,需要的朋友可以參考下2020-03-03

