日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++?Boost?StringAlgorithms超詳細講解_C 語言

作者:無水先生 ? 更新時間: 2022-12-13 編程語言

一、提要

boost C++對應的字符串對象也有一套標準操作方法。本文介紹庫Boost.StringAlgorithms的若干函數和功能示例。

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

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

三、應用Boost.StringAlgrithms庫

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

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

3.1 字符大小寫

示例1.將字符轉化成大寫(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';
}

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

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

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

示例2.使用語言環境將字符串轉換為大寫

#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 調用 boost::algorithm::to_upper_copy() 兩次以將土耳其語字符串“Boost C++ kütüphaneleri”轉換為大寫。第一次調用 boost::algorithm::to_upper_copy() 使用全局語言環境,在本例中為 C 語言環境。在 C 語言環境中,帶有變音符號的字符沒有大寫映射,因此輸出將如下所示:BOOST C++ KüTüPHANELERI。

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

注意:

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

3.2 刪除字符串內子串

示例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 提供了幾個函數,可用于從字符串中刪除單個字符(參見示例 3)。例如, boost::algorithm::erase_all_copy() 將從字符串中刪除所有出現的特定字符。要僅刪除第一次出現的字符,請改用 boost::algorithm::erase_first_copy()。要在任一端將字符串縮短特定數量的字符,請使用函數 boost::algorithm::erase_head_copy() 和 boost::algorithm::erase_tail_copy()。

3.3 查找字符串內子串

示例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';
}

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

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

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';
}

字符串容器作為第一個參數傳遞給函數 boost::algorithm::join(),該函數將它們連接起來,由第二個參數分隔。示例 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';
}

與搜索字符串或從字符串中刪除字符的函數一樣,Boost.StringAlgorithms 也提供了替換字符串中的子字符串的函數。其中包括以下函數: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()。它們可以以與搜索和刪除函數相同的方式應用,除了它們需要一個額外的參數 - 替換字符串(參見示例 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)。全局語言環境確定哪些字符被視為空格。

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

3.7 創立謂詞

示例8.創建謂詞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() 的函數,它是一個輔助函數,用于創建一個謂詞,用于檢查某個字符(作為參數傳遞給 is_any_of() )是否存在于字符串中。使用 boost::algorithm::is_any_of(),可以指定修剪字符串的字符。示例 5.8 使用連字符。

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

示例9.創建謂詞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() 返回的謂詞測試字符是否為數字。在示例9 中,boost::algorithm::is_digit() 用于從字符串 s 中刪除數字。

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

除了驗證字符串的單個字符的謂詞之外,Boost.StringAlgorithms 還提供了可以處理字符串的函數(參見示例 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() 函數比較兩個單獨的字符串。示例 11 引入了一個將字符串拆分為較小部分的函數。

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(),可以根據分隔符拆分給定的字符串。子字符串存儲在容器中。該函數需要一個謂詞作為其第三個參數,該謂詞測試每個字符并檢查字符串是否應在給定位置拆分。示例 5.11 使用輔助函數 boost::algorithm::is_space() 創建一個謂詞,在每個空格字符處分割字符串。

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

最后,Boost.StringAlgorithms 的很多函數也支持正則表達式。示例 5.12 使用函數 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++ 寫入標準輸出。

練習

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

原文鏈接:https://yamagota.blog.csdn.net/article/details/127127538

欄目分類
最近更新