C ++提升字符串的使用率

时间:2017-06-12 08:33:59

标签: c++ boost

我不知道提升,有人可以告诉我这个功能到底在做什么吗?

int
Function(const string& tempStr)
{
    boost::regex expression ("result = ");
    std::string::const_iterator start, end;
    start = tempStr.begin();
    end   = tempStr.end();
    boost::match_results<std::string::const_iterator> what;
    boost::regex_constants::_match_flags flags = boost::match_default;
    int count = 0;
    while(regex_search(start, end, what, expression, flags)){
        start = what[0].second;
        count++;
    }
    cout << "Count :"<< count << endl;
    return count;
}

4 个答案:

答案 0 :(得分:2)

match_resultssub_match个对象的集合。第一个sub_match对象(索引0)表示目标序列中的完全匹配(后续匹配将对应于子表达式匹配)。您的代码正在搜索result =个匹配项,并且每次从上一个匹配项结束时重新开始搜索(what[0].second

int
Function(const string& tempStr)
{
    boost::regex expression ("result = ");
    std::string::const_iterator start, end;
    start = tempStr.begin();
    end   = tempStr.end();
    boost::match_results<std::string::const_iterator> what;
    boost::regex_constants::_match_flags flags = boost::match_default;
    int count = 0;
    while(regex_search(start, end, what, expression, flags)){
        start = what[0].second;
        count++;
    }
    cout << "Count :"<< count << endl;
    return count;
}

int main()
{
    Function("result = 22, result = 33"); // Outputs 'Count: 2'
}

Live Example

答案 1 :(得分:0)

功能的基础是在tempStr上搜索正则表达式匹配。

查看regex_search文档并注意match_result在完成后包含的内容(这是第3个参数,或代码示例中的what)。从那里了解while循环应该是直截了当的。

答案 2 :(得分:0)

此函数是计算"result = "字符串出现次数的复杂方法。一种更简单的方法是:

boost::regex search_string("result = ");
auto begin = boost::make_regex_iterator(tempStr, search_string);
int count = std::distance(begin, {});

可以折叠为单行,可能会失去可读性。

答案 3 :(得分:0)

这是一个匹配计数器功能:

作者使用无用的代码:这是std(也是boost)中的等效代码

unsigned int count_match( std::string user_string, const std::string& user_pattern ){

    const std::regex rx( user_pattern );

    std::regex_token_iterator< std::string::const_iterator > first( user_string. begin(), user_string.end(), rx  ), last;

    return std::distance( first, last );

}  

std::regex_search它可以(也是提升):

unsigned int match_count( std::string user_string, const std::string& user_pattern ){
    unsigned int counter = 0;
    std::match_results< std::string::const_iterator > match_result;

    std::regex regex( user_pattern );

    while( std::regex_search( user_string, match_result, regex ) ){
        user_string = match_result.suffix().str();
        ++counter;
    }

    return counter;
}

注意:
无需使用此部分:

std::string::const_iterator start, end;
start = tempStr.begin();
end   = tempStr.end();

另外

boost::match_results<std::string::const_iterator> what;

可以

boost::smatch what // a typedef of match_results<std::string::const_iterator> 

不需要:

boost::regex_constants::_match_flags flags = boost::match_default;

因为默认情况下regex_search有此标志

这个:

start = what[0].second;

用于更新迭代,可以是:

match_result.suffix().str();   

如果你想看看while循环中发生了什么,请使用以下代码:

std::cout << "prefix: '" << what.prefix().str() << '\n';
std::cout << "match : '" << what.str() << '\n';
std::cout << "suffix: '" << what.suffix().str() << '\n';
std::cout << "------------------------------\n";
相关问题