boost string algorithm

The Boost.StringAlgorithms library provides many free-standing functions for string manipulation.ios

1. converting strings to uppercasegit

#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) << std::endl; return 0; }

The function boost::algorithm::to_upper_copy() converts a string to uppercase, and boost::algorithm::to_lower_copy() converts a string to lowecase. Both functions return a copy of the input string, converted to the specified case. To convert the string in place, use the functions boost::algorithm::to_upper() or boost::algorithm::to_lower().app

2. Algorithms to remove characters from a stringide

#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") << std::endl; 刪除第一個出現的s字符 std::cout << erase_nth_copy(s, "s", 0) << std::endl; std::cout << erase_last_copy(s, "s") << std::endl; std::cout << erase_all_copy(s, "s") << std::endl; std::cout << erase_head_copy(s, 5) << std::endl; 刪除從頭部開始後的5個字符 std::cout << erase_tail_copy(s, 9) << std::endl; 刪除從尾部開始的9個字符 return 0; }

輸出結果爲:ui

Boot C++ Librariesspa

Boot C++ Librariescode

Boost C++ Librarieblog

Boot C++ Librarieip

 C++ Librariesci

Boost C++

3. Searching for substrings

#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 << std::endl; r = find_first(s, "xyz"); std::cout << r << std::endl; return 0; }

輸出爲:

C++

functions such as boost::algorithm::find_first(), boost::algorithm::find_last(), boost::algorithm::find_nth(), boost::algorithm::find_heand() and boost::algorithm::find_tail(), these functions return a pair of iterators of type boost::iterator_range. Because the operator operator<< is overloaded for boost::iterator_range, the result of the individual search algorithm can be written directly to standard output.

4. Concatenatin strings with 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"}; join(v, " "); return 0; }

輸出爲:Boost C++ Libraries

5. replace characters in a string

functions: 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. trim strings

functions: boost::algorithm::trim_left_copy(), boost::algorithm::trim_right_copy(), boost::algorithm::trim_copy() to remove spaces on either end of a string.

Boost.StringAlgorithms lets you provide a predicate as an additional parameter for different functions to determine which characters of the string the function is applied to. The versions with predicate are: boost::algorithm::trim_right_copy_if(), boost::algorithm::trim_left_copy_if(), boost::algorithm::trim_copy_if()

7. creating predicates with 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("-")) << std::endl; std::cout << trim_right_copy_if(s, is_any_of("-")) << std::endl; std::cout << trim_copy_if(s, is_any_of("-")) << std::endl; return 0; }

輸出爲:

Boost C++ Libraries--

--Boost C++ Libraries

Boost C++ Libraries

function called boost::algorithm::is_any_of(), which is a helper function to create a predicate that checks whether a certain character--passed as parameter to is_any_of() exists in a string. With boost::algorithm::is_any_of(), the characters for trimming a string can be specified.

many helper functions that return commonly used predicates: boost::algorithm::is_digit(), boost::algorithm::is_upper(), boost::algorithm::is_lower().

8. Algorithms to compare strings with others

#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") << std::endl; std::cout << ends_with(s, "Libraries") << std::endl; std::cout << contains(s, "C++") << std::endl; std::cout << lexicographical_compare(s, "Boost") << std::endl; return 0; }

輸出爲:

true

true

true

false

9. Splitting strings with 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() << std::endl; return 0; }

a given string can be split based on a delimiter. The substrings are stored in a container. The function requires as its third parameter a predicate that tests each character an checks whether the string should be split at the given position.

10. Searching strings with 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 << std::endl; return 0; }

輸出爲:C++

相關文章
相關標籤/搜索