2017-06-12 71 views
2

我不知道增强,有人可以告诉我这个函数到底在做什么吗?C++提升字符串的使用率

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; 
} 
+1

在旁注中,使用正则表达式是过度的,因为这可能只是做ne使用'std :: string :: find()',这会获得更好的性能。 – zett42

回答

0

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

查看regex_search文档,并注意match_result包含的内容(代码示例中的第3个参数或what)。从那里了解while循环应该很简单。

+0

如果可能的话,你可以用一个例子来解释一下吗? – instance

+0

你为什么不做一个简单的测试? @ Marco的答案包括一个 – sehe

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

0

此功能的一端重新搜索是计算出现的次数比较复杂了"result = "字符串。更简单的方法是:

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

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

0

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

笔者采用无用代码:这里是性病的等效代码(也助推)

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";