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

C++?Boost?StringAlgorithms超詳細講解

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

一、提要

boost C++對應(yīng)的字符串對象也有一套標(biāo)準(zhǔn)操作方法。本文介紹庫Boost.StringAlgorithms的若干函數(shù)和功能示例。

二、簡化字符串處理的工具和其庫

  • Boost.StringAlgorithms 定義了許多專門針對字符串的算法。例如,您會找到將字符串轉(zhuǎn)換為小寫或大寫的算法。
  • Boost.LexicalCast 提供了一個轉(zhuǎn)換運算符來將數(shù)字轉(zhuǎn)換為字符串,反之亦然。該庫在內(nèi)部使用字符串流,但可能針對某些類型之間的轉(zhuǎn)換進行了優(yōu)化。
  • Boost.Format 為 std::printf() 提供了一個類型安全的替代方案。像 Boost.LexicalCast 一樣,這個庫在內(nèi)部使用字符串流。如果定義了輸出流操作符,Boost.Format 是可擴展的并且支持用戶定義的類型。
  • Boost.Regex 和 Boost.Xpressive 是使用正則表達式在字符串中搜索的庫。雖然 Boost.Regex 期望將正則表達式寫成字符串,但 Boost.Xpressive 允許您將它們寫成 C++ 代碼。
  • Boost.Tokenizer 使得迭代字符串中的子字符串成為可能。
  • Boost.Spirit 可用于開發(fā)基于類似于 Extended Backus-Naur-Form 規(guī)則的解析器。

三、應(yīng)用Boost.StringAlgrithms庫

Boost.StringAlgorithms 庫為字符串操作提供了許多獨立的函數(shù)。字符串的類型可以是 std::string、std::wstring 或類模板 std::basic_string 的任何其他實例。這包括 C++11 引入的字符串類 std::u16string 和 std::u32string。

這些函數(shù)被分類在不同的頭文件中。例如,從大寫轉(zhuǎn)換為小寫的函數(shù)在 boost/algorithm/string/case_conv.hpp 中定義。因為 Boost.StringAlgorithms 包含 20 多個不同的類別和盡可能多的頭文件,為了方便起見,boost/algorithm/string.hpp 充當(dāng)通用頭文件,包括所有其他頭文件。

3.1 字符大小寫

示例1.將字符轉(zhuǎn)化成大寫(to_upper_copy)

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  std::cout << to_upper_copy(s) << '\n';
}

函數(shù) boost::algorithm::to_upper_copy() 將字符串轉(zhuǎn)換為大寫,boost::algorithm::to_lower_copy() 將字符串轉(zhuǎn)換為小寫。這兩個函數(shù)都返回輸入字符串的副本,轉(zhuǎn)換為指定的大小寫。要就地轉(zhuǎn)換字符串,請使用函數(shù) boost::algorithm::to_upper() 或 boost::algorithm::to_lower()。

示例 1 使用 boost::algorithm::to_upper_copy() 將字符串“Boost C++ Libraries”轉(zhuǎn)換為大寫。該示例將 BOOST C++ LIBRARIES 寫入標(biāo)準(zhǔn)輸出。

Boost.StringAlgorithms 中的函數(shù)會考慮語言環(huán)境。如果沒有將語言環(huán)境作為參數(shù)顯式傳遞,則諸如 boost::algorithm::to_upper_copy() 之類的函數(shù)會使用全局語言環(huán)境。

示例2.使用語言環(huán)境將字符串轉(zhuǎn)換為大寫

#include <boost/algorithm/string.hpp>
#include <string>
#include <locale>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ k\xfct\xfcphaneleri";
  std::string upper_case1 = to_upper_copy(s);
  std::string upper_case2 = to_upper_copy(s, std::locale{"Turkish"});
  std::locale::global(std::locale{"Turkish"});
  std::cout << upper_case1 << '\n';
  std::cout << upper_case2 << '\n';
}

示例 2 調(diào)用 boost::algorithm::to_upper_copy() 兩次以將土耳其語字符串“Boost C++ kütüphaneleri”轉(zhuǎn)換為大寫。第一次調(diào)用 boost::algorithm::to_upper_copy() 使用全局語言環(huán)境,在本例中為 C 語言環(huán)境。在 C 語言環(huán)境中,帶有變音符號的字符沒有大寫映射,因此輸出將如下所示:BOOST C++ KüTüPHANELERI。

土耳其語語言環(huán)境被傳遞給對 boost::algorithm::to_upper_copy() 的第二次調(diào)用。由于此語言環(huán)境確實具有變音符號的大寫等效項,因此可以將整個字符串轉(zhuǎn)換為大寫。因此,對 boost::algorithm::to_upper_copy() 的第二次調(diào)用正確地轉(zhuǎn)換了字符串,如下所示:BOOST C++ KÜTÜPHANELERI。

注意:

如果您想在 POSIX 操作系統(tǒng)上運行示例,請將“Turkish”替換為“tr_TR”,并確保安裝了土耳其語區(qū)域設(shè)置。

3.2 刪除字符串內(nèi)子串

示例3.從字符串中刪除字符的算法

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  std::cout << erase_first_copy(s, "s") << '\n';
  std::cout << erase_nth_copy(s, "s", 0) << '\n';
  std::cout << erase_last_copy(s, "s") << '\n';
  std::cout << erase_all_copy(s, "s") << '\n';
  std::cout << erase_head_copy(s, 5) << '\n';
  std::cout << erase_tail_copy(s, 9) << '\n';
}

Boost.StringAlgorithms 提供了幾個函數(shù),可用于從字符串中刪除單個字符(參見示例 3)。例如, boost::algorithm::erase_all_copy() 將從字符串中刪除所有出現(xiàn)的特定字符。要僅刪除第一次出現(xiàn)的字符,請改用 boost::algorithm::erase_first_copy()。要在任一端將字符串縮短特定數(shù)量的字符,請使用函數(shù) boost::algorithm::erase_head_copy() 和 boost::algorithm::erase_tail_copy()。

3.3 查找字符串內(nèi)子串

示例4. 查找字串boost::algorithm::find_first()

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  boost::iterator_range<std::string::iterator> r = find_first(s, "C++");
  std::cout << r << '\n';
  r = find_first(s, "xyz");
  std::cout << r << '\n';
}

函數(shù)如 boost::algorithm::find_first()、boost::algorithm::find_last()、boost::algorithm::find_nth()、boost::algorithm::find_head() 和 boost::algorithm::find_tail () 可用于在字符串中查找字符串。

所有這些函數(shù)都返回一對 boost::iterator_range 類型的迭代器。這個類源自Boost.Range,它實現(xiàn)了基于迭代器概念的范圍概念。由于運算符 operator<< 為 boost::iterator_range 重載,因此單個搜索算法的結(jié)果可以直接寫入標(biāo)準(zhǔn)輸出。示例 4 為第一個結(jié)果打印 C++,為第二個結(jié)果打印一個空字符串。

3.4 合并字符串

示例.5. 合并字符串boost::algorithm::join()

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::vector<std::string> v{"Boost", "C++", "Libraries"};
  std::cout << join(v, " ") << '\n';
}

字符串容器作為第一個參數(shù)傳遞給函數(shù) boost::algorithm::join(),該函數(shù)將它們連接起來,由第二個參數(shù)分隔。示例 5.5 將輸出 Boost C++ 庫。

3.5 子串替換

示例6.替換字符串中字符的算法

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  std::cout << replace_first_copy(s, "+", "-") << '\n';
  std::cout << replace_nth_copy(s, "+", 0, "-") << '\n';
  std::cout << replace_last_copy(s, "+", "-") << '\n';
  std::cout << replace_all_copy(s, "+", "-") << '\n';
  std::cout << replace_head_copy(s, 5, "BOOST") << '\n';
  std::cout << replace_tail_copy(s, 9, "LIBRARIES") << '\n';
}

與搜索字符串或從字符串中刪除字符的函數(shù)一樣,Boost.StringAlgorithms 也提供了替換字符串中的子字符串的函數(shù)。其中包括以下函數(shù):boost::algorithm::replace_first_copy()、boost::algorithm::replace_nth_copy()、boost::algorithm::replace_last_copy()、boost::algorithm::replace_all_copy()、boost::algorithm ::replace_head_copy() 和 boost::algorithm::replace_tail_copy()。它們可以以與搜索和刪除函數(shù)相同的方式應(yīng)用,除了它們需要一個額外的參數(shù) - 替換字符串(參見示例 6)。

3.6 字符串修剪

示例7.修剪字符串的算法

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "\t Boost C++ Libraries \t";
  std::cout << "_" << trim_left_copy(s) << "_\n";
  std::cout << "_" << trim_right_copy(s) << "_\n";
  std::cout << "_" << trim_copy(s) << "_\n";
}

要刪除字符串兩端的空格,請使用 boost::algorithm::trim_left_copy()、boost::algorithm::trim_right_copy() 和 boost::algorithm::trim_copy()(參見示例 5.7)。全局語言環(huán)境確定哪些字符被視為空格。

Boost.StringAlgorithms 允許您提供謂詞作為不同函數(shù)的附加參數(shù),以確定函數(shù)應(yīng)用于字符串的哪些字符。帶有謂詞的版本是:boost::algorithm::trim_right_copy_if()、boost::algorithm::trim_left_copy_if() 和 boost::algorithm::trim_copy_if()。

3.7 創(chuàng)立謂詞

示例8.創(chuàng)建謂詞1is_any_of

boost::algorithm::is_any_of()

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "--Boost C++ Libraries--";
  std::cout << trim_left_copy_if(s, is_any_of("-")) << '\n';
  std::cout << trim_right_copy_if(s, is_any_of("-")) << '\n';
  std::cout << trim_copy_if(s, is_any_of("-")) << '\n';
}

示例 8 使用另一個名為 boost::algorithm::is_any_of() 的函數(shù),它是一個輔助函數(shù),用于創(chuàng)建一個謂詞,用于檢查某個字符(作為參數(shù)傳遞給 is_any_of() )是否存在于字符串中。使用 boost::algorithm::is_any_of(),可以指定修剪字符串的字符。示例 5.8 使用連字符。

Boost.StringAlgorithms 提供了許多返回常用謂詞的輔助函數(shù)。

示例9.創(chuàng)建謂詞2(is_digit() )

boost::algorithm::is_digit()

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "123456789Boost C++ Libraries123456789";
  std::cout << trim_left_copy_if(s, is_digit()) << '\n';
  std::cout << trim_right_copy_if(s, is_digit()) << '\n';
  std::cout << trim_copy_if(s, is_digit()) << '\n';
}

boost::algorithm::is_digit() 返回的謂詞測試字符是否為數(shù)字。在示例9 中,boost::algorithm::is_digit() 用于從字符串 s 中刪除數(shù)字。

Boost.StringAlgorithms 還提供了輔助函數(shù)來檢查字符是大寫還是小寫:boost::algorithm::is_upper() 和 boost::algorithm::is_lower()。默認情況下,所有這些函數(shù)都使用全局語言環(huán)境,除非您將不同的語言環(huán)境作為參數(shù)傳遞。

除了驗證字符串的單個字符的謂詞之外,Boost.StringAlgorithms 還提供了可以處理字符串的函數(shù)(參見示例 10)。

3.8 比較

示例10.字符串的比較

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  std::cout.setf(std::ios::boolalpha);
  std::cout << starts_with(s, "Boost") << '\n';
  std::cout << ends_with(s, "Libraries") << '\n';
  std::cout << contains(s, "C++") << '\n';
  std::cout << lexicographical_compare(s, "Boost") << '\n';
}

boost::algorithm::starts_with()、boost::algorithm::ends_with()、boost::algorithm::contains() 和 boost::algorithm::lexicographical_compare() 函數(shù)比較兩個單獨的字符串。示例 11 引入了一個將字符串拆分為較小部分的函數(shù)。

3.9 拆分字符串

示例 11.拆分字符串boost::algorithm::split()

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  std::vector<std::string> v;
  split(v, s, is_space());
  std::cout << v.size() << '\n';
}

使用 boost::algorithm::split(),可以根據(jù)分隔符拆分給定的字符串。子字符串存儲在容器中。該函數(shù)需要一個謂詞作為其第三個參數(shù),該謂詞測試每個字符并檢查字符串是否應(yīng)在給定位置拆分。示例 5.11 使用輔助函數(shù) boost::algorithm::is_space() 創(chuàng)建一個謂詞,在每個空格字符處分割字符串。

本章介紹的許多函數(shù)都有忽略字符串大小寫的版本。這些版本通常具有相同的名稱,但前導(dǎo)“i”除外。例如,boost::algorithm::erase_all_copy() 的等效函數(shù)是 boost::algorithm::ierase_all_copy()。

最后,Boost.StringAlgorithms 的很多函數(shù)也支持正則表達式。示例 5.12 使用函數(shù) boost::algorithm::find_regex() 來搜索正則表達式。

3.10 查找字符串

示例12.字符串查找boost::algorithm::find_regex()

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <string>
#include <iostream>
using namespace boost::algorithm;
int main()
{
  std::string s = "Boost C++ Libraries";
  boost::iterator_range<std::string::iterator> r =
    find_regex(s, boost::regex{"\\w\\+\\+"});
  std::cout << r << '\n';
}

為了使用正則表達式,程序會訪問一個名為 boost::regex 的類,該類將在第 8 章中介紹。

示例 12 將 C++ 寫入標(biāo)準(zhǔn)輸出。

練習(xí)

創(chuàng)建一個程序,要求用戶輸入他的全名。程序應(yīng)該用“Hello”來問候用戶,然后是用戶名和感嘆號。用戶的名字和姓氏應(yīng)以大寫字母開頭,后跟小寫字母。此外,用戶的名字和姓氏應(yīng)該用一個空格隔開。感嘆號前不能有空格。

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

相關(guān)文章

  • C語言如何建立鏈表并實現(xiàn)增刪查改詳解

    C語言如何建立鏈表并實現(xiàn)增刪查改詳解

    這篇文章主要給大家介紹了關(guān)于C語言如何建立鏈表并實現(xiàn)增刪查改的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C語言具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • C++實現(xiàn)LeetCode(8.字符串轉(zhuǎn)為整數(shù))

    C++實現(xiàn)LeetCode(8.字符串轉(zhuǎn)為整數(shù))

    這篇文章主要介紹了C++實現(xiàn)LeetCode(8.字符串轉(zhuǎn)為整數(shù)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言字符串函數(shù),字符函數(shù),內(nèi)存函數(shù)使用及模擬實現(xiàn)

    C語言字符串函數(shù),字符函數(shù),內(nèi)存函數(shù)使用及模擬實現(xiàn)

    這篇文章主要介紹了C語言字符串函數(shù),字符函數(shù),內(nèi)存函數(shù)使用及模擬實現(xiàn),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 解析C++中的虛擬函數(shù)及其靜態(tài)類型和動態(tài)類型

    解析C++中的虛擬函數(shù)及其靜態(tài)類型和動態(tài)類型

    虛擬函數(shù)(Visual Function)亦常被成為虛函數(shù),是C++中的一個重要特性,本文我們就來解析C++中的虛擬函數(shù)及其靜態(tài)類型和動態(tài)類型
    2016-06-06
  • Qt QML使用虛擬鍵盤的示例代碼

    Qt QML使用虛擬鍵盤的示例代碼

    這篇文章主要為大家詳細介紹了Qt QML使用虛擬鍵盤的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 深入淺出理解C語言初識結(jié)構(gòu)體

    深入淺出理解C語言初識結(jié)構(gòu)體

    C?數(shù)組允許定義可存儲相同類型數(shù)據(jù)項的變量,結(jié)構(gòu)是?C?編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許你存儲不同類型的數(shù)據(jù)項,本篇讓我們來了解C?的結(jié)構(gòu)體
    2022-02-02
  • Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)通用數(shù)據(jù)庫請求

    Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)通用數(shù)據(jù)庫請求

    這篇文章主要為大家介紹了Qt中是如何實現(xiàn)通用數(shù)據(jù)庫請求的,文中的示例代碼講解詳細,對我們學(xué)習(xí)Qt有一定幫助,感興趣的小伙伴可以了解一下
    2022-03-03
  • 利用c++編寫簡易版2048小游戲

    利用c++編寫簡易版2048小游戲

    這篇文章主要介紹了如何讓利用c++編寫簡易版的2048小游戲,感興趣的小伙伴請參考下面文章的具體內(nèi)容
    2021-09-09
  • C語言數(shù)組全面詳細講解

    C語言數(shù)組全面詳細講解

    數(shù)組是一組有序的數(shù)據(jù)的集合,數(shù)組中元素類型相同,由數(shù)組名和下標(biāo)唯一地確定,數(shù)組中數(shù)據(jù)不僅數(shù)據(jù)類型相同,而且在計算機內(nèi)存里連續(xù)存放,地址編號最低的存儲單元存放數(shù)組的起始元素,地址編號最高的存儲單元存放數(shù)組的最后一個元素
    2022-05-05
  • C++示例講解觀察者設(shè)計模式

    C++示例講解觀察者設(shè)計模式

    觀察者模式是極其重要的一個設(shè)計模式,也是我?guī)啄觊_發(fā)過程中使用最多的設(shè)計模式,本文首先概述觀察者模式的基本概念和Demo實現(xiàn),接著是觀察者模式在C++中的應(yīng)用,最后是對觀察者模式的應(yīng)用場景和優(yōu)缺點進行總結(jié)
    2022-12-12

最新評論