xunwap

专注移动互联网服务

正在浏览 Boost 里的文章

用std::string来代替char*作为字符串处理的方式在现代的C++开发中已是约定俗成的规则了。不过std::string的公有成员函数的功能是很有限的,而且操作起来并不是很方便,借助于 std::algorithms 来操作std::string时,因为std::algorithms需要面对的是容器的通用操作,也以,也有部分我们所需要的std::string操作无法通过std::algorithms来实现。好在 String Algorithms Library 为std::string弥补了众多在操作上的不足之处。先看看 String Algorithms Library 的函数:

Algorithms

Table 21.1. Case Conversion

Algorithm name Description Functions
to_upper Convert a string to upper case to_upper_copy()
to_upper()
to_lower Convert a string to lower case to_lower_copy()
to_lower()

Table 21.2. Trimming

Algorithm name Description Functions
trim_left Remove leading spaces from a string trim_left_copy_if()
trim_left_if()
trim_left_copy()
trim_left()
trim_right Remove trailing spaces from a string trim_right_copy_if()
trim_right_if()
trim_right_copy()
trim_right()
trim Remove leading and trailing spaces from a string trim_copy_if()
trim_if()
trim_copy()
trim()

Table 21.3. Predicates

Algorithm name Description Functions
starts_with Check if a string is a prefix of the other one starts_with()
istarts_with()
ends_with Check if a string is a suffix of the other one ends_with()
iends_with()
contains Check if a string is contained of the other one contains()
icontains()
equals Check if two strings are equal equals()
iequals()
lexicographical_compare Check if a string is lexicographically less then another one lexicographical_compare()
ilexicographical_compare()
all Check if all elements of a string satisfy the given predicate all()

Table 21.4. Find algorithms

Algorithm name Description Functions
find_first Find the first occurrence of a string in the input find_first()
ifind_first()
find_last Find the last occurrence of a string in the input find_last()
ifind_last()
find_nth Find the nth (zero-indexed) occurrence of a string in the input find_nth()
ifind_nth()
find_head Retrieve the head of a string find_head()
find_tail Retrieve the tail of a string find_tail()
find_token Find first matching token in the string find_token()
find_regex Use the regular expression to search the string find_regex()
find Generic find algorithm find()

Table 21.5. Erase/Replace

Algorithm name Description Functions
replace/erase_first Replace/Erase the first occurrence of a string in the input replace_first()
replace_first_copy()
ireplace_first()
ireplace_first_copy()
erase_first()
erase_first_copy()
ierase_first()
ierase_first_copy()
replace/erase_last Replace/Erase the last occurrence of a string in the input replace_last()
replace_last_copy()
ireplace_last()
ireplace_last_copy()
erase_last()
erase_last_copy()
ierase_last()
ierase_last_copy()
replace/erase_nth Replace/Erase the nth (zero-indexed) occurrence of a string in the input replace_nth()
replace_nth_copy()
ireplace_nth()
ireplace_nth_copy()
erase_nth()
erase_nth_copy()
ierase_nth()
ierase_nth_copy()
replace/erase_all Replace/Erase the all occurrences of a string in the input replace_all()
replace_all_copy()
ireplace_all()
ireplace_all_copy()
erase_all()
erase_all_copy()
ierase_all()
ierase_all_copy()
replace/erase_head Replace/Erase the head of the input replace_head()
replace_head_copy()
erase_head()
erase_head_copy()
replace/erase_tail Replace/Erase the tail of the input replace_tail()
replace_tail_copy()
erase_tail()
erase_tail_copy()
replace/erase_regex Replace/Erase a substring matching the given regular expression replace_regex()
replace_regex_copy()
erase_regex()
erase_regex_copy()
replace/erase_regex_all Replace/Erase all substrings matching the given regular expression replace_all_regex()
replace_all_regex_copy()
erase_all_regex()
erase_all_regex_copy()
find_format Generic replace algorithm find_format()
find_format_copy()
find_format_all()
find_format_all_copy()()

Table 21.6. Split

Algorithm name Description Functions
find_all Find/Extract all matching substrings in the input find_all()
ifind_all()
find_all_regex()
split Split input into parts split()
split_regex()
iter_find Iteratively apply the finder to the input to find all matching substrings iter_find()
iter_split Use the finder to find matching substrings in the input and use them as separators to split the input into parts iter_split()

Table 21.7. Join

Algorithm name Description Functions
join Join all elements in a container into a single string join
join_if Join all elements in a container that satisfies the condition into a single string join_if()

Boost.String_Algo 的函数功能是相当的丰富了,也简便易用。所以可以在开发过程中大量使用。

BTW:有很多的人都排斥Boost,在一开始的时候我也不使用Boost,因为有太多的人建议我不要用。后来我还是使用了Boost,而且越用越喜欢,也越发感觉到Boost是一个多么灵活,高效,强大的C++ Library;也体会到了为什么可以称Boost具有”工业强度”。在这里,我建议那些不愿意使用Boost的朋友去使用一下Boost,也许你们是听别人建议不要用Boost,也许是自己不愿意使用Boost,但你们一定要去试用Boost中的一些可能用得上的Library,也许用了之后你们的看法会有所改变,也许你们会有不同的发现 :)

本站原创文章,转载请注明出处

下面是一个使用boost.property_tree来解析XML/INI文件的简单示例。
使用boost.property_tree来作为配置文件的解析工具非常合适.
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <set>

#include <stdlib.h>
#include <unistd.h>

#include <boost/foreach.hpp>
#include <boost/noncopyable.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>

int main( int argc , char *argv[] )
{
    std::string strConfigName( "" );
    std::string strReportName( "" );

    const char *szShortOptions = ":f:t:h";
    while ( true )
    {
        const int iOption = getopt( argc , argv , szShortOptions );
        if ( -1 == iOption )
        {
            break;
        }

        switch ( iOption )
        {
        case 'f':
        {
            strConfigName = optarg;
        }
        break;

        case 't':
        {
            strReportName = optarg;
        }
        break;

        case ':':
        {
            std::cerr << "-" << static_cast< char >( iOption ) \
                      << " need option value." << std::endl;
        }
        break;

        case 'h':
        {
            std::cout << "usage: " << argv[ 0 ] << " -f config [-h]"
                      << std::endl;
            std::cout << "-f config     specify the config file name." \
                      << std::endl;
            std::cout << "-t report     specify the target report name." \
                      << std::endl;
            std::cout << "-h            show the help information." \
                      << std::endl;

        }
        return EXIT_SUCCESS;

        case '?':
        default:
        {
            std::cerr << "unknown option: " << static_cast< char >( iOption ) \
                      << std::endl;
        }
        break;

        }
    }

    if ( ( true == strConfigName.empty() ) \
            || ( true == strReportName.empty() ) )
    {
        std::cout << "ERROR, usage: " << argv[ 0 ] \
                  << " -f config" \
                  << "\t" \
                  << " -t report"
                  << std::endl;
        return -1;
    }

    /*

        typedef ::boost::property_tree::ptree IniParser;
        IniParser iniParser;

        const std::string strNullString = "";
        const int iDefaultIntValue = 0;

        read_ini( strConfigName , iniParser );
        const std::string strLogName = iniParser.get( "debug.log_name" , strNullString );
        const int iLogLevel = iniParser.get( "debug.level" , iDefaultIntValue );

        typedef std::set< std::string > ModuleSet;
        ModuleSet moduleSet;

        BOOST_FOREACH( IniParser::value_type &node , \
                iniParser.get_child( "modules" ) )
        {
                moduleSet.insert( node.second.data() );
        }

        std::string strIndex( "1" );

        IniParser reportINI;
        reportINI.put( "debug.log_name" , strLogName );
        reportINI.put( "debug.level" , iLogLevel);
        BOOST_FOREACH( const std::string &strName , moduleSet )
        {
                reportINI.put( "modules.module" + strIndex , \
                        strName );
                strIndex[ 0 ] += 1;
        }

        write_ini( strReportName , reportINI );
    */

    typedef ::boost::property_tree::ptree XmlParser;
    XmlParser xmlParser;

    const std::string strNullString = "";
    const int iDefaultIntValue = 0;

    read_xml( strConfigName , xmlParser );
    const std::string strLogName = xmlParser.get( "debug.log_name" , strNullString );
    const int iLogLevel = xmlParser.get( "debug.level" , iDefaultIntValue );

    typedef std::set< std::string > ModuleSet;
    ModuleSet moduleSet;

    BOOST_FOREACH( XmlParser::value_type &node , \
                   xmlParser.get_child( "debug.modules" ) )
    {
        moduleSet.insert( node.second.data() );
    }

    XmlParser reportXml;
    reportXml.put( "debug.log_name" , strLogName );
    reportXml.put( "debug.level" , iLogLevel);
    BOOST_FOREACH( const std::string &strName , moduleSet )
    {
        reportXml.put( "debug.modules.module" , \
                       strName );
    }

    write_xml( strReportName , reportXml );

    return EXIT_SUCCESS;

}

本站原创文章,转载请注明出处。