当前位置: 首页 > 编程笔记 >

C++之BOOST字符串查找示例

万俟高峻
2023-03-14
本文向大家介绍C++之BOOST字符串查找示例,包括了C++之BOOST字符串查找示例的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了C++中BOOST字符串查找的方法,分享给大家供大家参考。具体方法如下:

BOOST  字符串查找示例

#include <string>  

#include <iostream>  

#include <algorithm>  

#include <functional>  

#include <boost/algorithm/string/case_conv.hpp>  

#include <boost/algorithm/string/find.hpp>  

  

using namespace std;  

using namespace boost;  

  

int main()  

{    

    cout << "* Find Example *" << endl << endl;  

  

    string str1("abc___cde___efg");  

    string str2("abc");  

  

    // find "cde" substring  

    iterator_range<string::iterator> range=find_first( str1, string("cde") );  

  

    // convert a substring to upper case   

    // note that iterator range can be directly passed to the algorithm  

    to_upper( range );  

  

    cout << "str1 with upper-cased part matching cde: " << str1 << endl;  

  

    // get a head of the string  

    iterator_range<string::iterator> head=find_head( str1, 3 );  

    cout << "head(3) of the str1: " << string( head.begin(), head.end() ) << endl;  

  

    // get the tail  

    head=find_tail( str2, 5 );  

    cout << "tail(5) of the str2: " << string( head.begin(), head.end() ) << endl;  

  

    // char processing  

    char text[]="hello dolly!";  

    iterator_range<char*> crange=find_last(text,"ll");  

  

    // transform the range ( add 1 )  

    transform( crange.begin(), crange.end(), crange.begin(), bind2nd( plus<char>(), 1 ) );  

    // uppercase the range  

    to_upper( crange );  

  

    cout << text << endl;  

  

    cout << endl;  

  

    return 0;  

}

boost 判定函数的使用

#include <string>  

#include <iostream>  

#include <functional>  

#include <boost/algorithm/string/predicate.hpp>  

#include <boost/algorithm/string/classification.hpp>  

#include <boost/bind.hpp> 

using namespace std;  using namespace boost; 

int main()  {      cout << "* Predicate Example *" << endl << endl;        string str1("123xxx321");      string str2("abc");        // Check if str1 starts with '123'      cout << "str1 starts with \"123\": " <<           (starts_with( str1, string("123") )?"true":"false") << endl;             // Check if str1 ends with '123'      cout << "str1 ends with \"123\": " <<           (ends_with( str1, string("123") )?"true":"false") << endl;         // Check if str1 containes 'xxx'      cout << "str1 contains \"xxx\": " <<           (contains( str1, string("xxx") )?"true":"false") << endl;         // Check if str2 equals to 'abc'      cout << "str2 equals \"abc\": " <<           (equals( str2, string("abc") )?"true":"false") << endl;         // Classification functors and all predicate      if ( all(";.,", is_punct() ) )      {          cout << "\";.,\" are all punctuation characters" << endl;        }        // Classification predicates can be combined       if ( all("abcxxx", is_any_of("xabc") && !is_space() ) )      {          cout << "true" << endl;      }        cout << endl;        return 0;  }

boost替换示例

#include <string>  

#include <iostream>  

#include <iterator>  

//#include <boost/algorithm/string/replace.hpp>  

//#include <boost/algorithm/string/erase.hpp>  

//#include <boost/algorithm/string/case_conv.hpp>  

#include <boost/algorithm/string.hpp>  

  

//Following two includes contain second-layer function.  

//They are already included by first-layer header  

  

//#include <boost/algorithm/string/replace2.hpp>  

//#include <boost/algorithm/string/find2.hpp>  

  

using namespace std;  

using namespace boost;  

  

// uppercase formatter  

/*  

    Convert an input to upper case.   

    Note, that this formatter can be used only on std::string inputs.  

*/  

inline string upcase_formatter(   

    const iterator_range<string::const_iterator>& Replace )  

{  

    string Temp(Replace.begin(), Replace.end());  

    to_upper(Temp);  

    return Temp;  

}  

  

int main()  

{    

    cout << "* Replace Example *" << endl << endl;  

  

    string str1("abc___cde___efg");  

  

    // Erase 6-9th characters from the string  

    cout << "str1 without 6th to 9th character:" <<  

        erase_range_copy( str1, make_iterator_range(str1.begin()+6, str1.begin()+9) ) << endl;  

  

    // Replace 6-9th character with '+++'  

    cout << "str1 with 6th to 9th character replaced with '+++': " <<   

        replace_range_copy(   

            str1, make_iterator_range(str1.begin()+6, str1.begin()+9), "+++" ) << endl;  

  

    cout << "str1 with 'cde' replaced with 'XYZ': ";  

      

    // Replace first 'cde' with 'XYZ'. Modify the input  

    replace_first_copy( ostream_iterator<char>(cout), str1, "cde", "XYZ" );  

    cout << endl;  

      

    // Replace all '___'  

    cout << "str1 with all '___' replaced with '---': " <<   

        replace_all_copy( str1, "___", "---" ) << endl;  

  

    // Erase all '___'  

    cout << "str1 without all '___': " <<   

        erase_all_copy( str1, "___" ) << endl;  

  

    // replace third and 5th occurrence of _ in str1  

    // note that nth argument is 0-based  

    replace_nth( str1, "_", 4, "+" );  

    replace_nth( str1, "_", 2, "+" );  

  

    cout << "str1 with third and 5th occurrence of _ replace: " << str1 << endl;  

  

    // Custom formatter examples  

    string str2("abC-xxxx-AbC-xxxx-abc");  

  

    // Find string 'abc' ignoring the case and convert it to upper case  

    cout << "Upcase all 'abc'(s) in the str2: " <<  

        find_format_all_copy(   

            str2,  

            first_finder("abc", is_iequal()),   

            upcase_formatter );  

      

    cout << endl;  

  

    return 0;  

}

希望本文所述对大家的C++程序设计有所帮助。

 类似资料:
  • 问题内容: 如何找到两个子字符串之间的字符串? 我当前的方法是这样的: 但是,这似乎效率很低而且不合Python。什么是做这样的更好的方法? 忘了提:该字符串可能无法启动,并最终和。他们之前和之后的字符可能更多。 问题答案:

  • 问题内容: 我试图找到一些例子,但没有运气。有人知道网上的一些例子吗?我想知道当找不到时返回的内容,以及如何从头到尾指定,我猜这将是0,-1。 问题答案: 您也可以使用:

  • 字符串实际上是由null字符'\ 0'终止的一维字符数组。 因此,以null结尾的字符串包含组成字符串后跟null 。 以下声明和初始化创建一个由单词“Hello”组成的字符串。 要将空字符保存在数组的末尾,包含字符串的字符数组的大小比单词“Hello”中的字符数多一个。 char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; 如果你遵循数组初始化

  • 我有一个这样的字符串: 我正在尝试获取任何显示为title(title=“anything here”)的内容。我已经尝试过了,但无法正常工作。

  • 主要内容:根据字符查找,根据索引查找在给定的字符串中查找字符或字符串是比较常见的操作。字符串查找分为两种形式:一种是在字符串中获取匹配字符(串)的索引值,另一种是在字符串中获取指定索引位置的字符。 根据字符查找 String 类的 indexOf() 方法和 lastlndexOf() 方法用于在字符串中获取匹配字符(串)的索引值。 1. indexOf() 方法 indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索

  • 问题 你需要搜索一个字符串,并返回匹配的起始位置或匹配值本身。 解决方案 有几种使用正则表达式的方法来实现这个功能。其中一些方法被称为 RegExp 模式或对象还有一些方法被称为 String 对象。 RegExp 对象 第一种方式是在 RegExp 模式或对象中调用 test 方法。test 方法返回一个布尔值: match = /sample/.test("Sample text") # =>