代码之家  ›  专栏  ›  技术社区  ›  Peter VARGA

std::regex_match与另一个分配器

  •  1
  • Peter VARGA  · 技术社区  · 8 年前

    我不知道如何使用 regex_match 模板和我自己的内存STL分配器。

    这是我的代码:

    FaF::smatch stringResults;
    std::regex expression( "expression" );
    std::regex_match( FaF::string-variable, stringResults, expression );
    

    std::match std::string 我很成功,因此我在上面的例子中使用了它:

    namespace FaF
    {
        using smatch = std::match_results<std::string::const_iterator,
                                              Allocator<std::string::const_iterator>>;
        using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
    }
    

    我的分配器有一些日志记录,我可以清楚地看到,是的,它确实被使用了。 当我了解 cppreference 正确,那么 std::regex 没有分配器,但 std::regex_match does .

    我的问题:

    如何根据上述类型在 namespace FaF 基于的附加模板 标准::regex匹配

    3 回复  |  直到 8 年前
        1
  •  1
  •   Peter VARGA    8 年前

    之后 研究 std::regex_match regex.h

      template<typename _Ch_traits, typename _Ch_alloc,
           typename _Alloc, typename _Ch_type, typename _Rx_traits>
        inline bool
        regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
            match_results<typename basic_string<_Ch_type,
            _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
            const basic_regex<_Ch_type, _Rx_traits>& __re,
            regex_constants::match_flag_type __flags
            = regex_constants::match_default)
        { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
    

    我理解并意识到我自己对 FaF::string 并定义我自己 FaF::smatch [定义在问题中]就足够了,因为 _Alloc

    我的代码如下:

    void getBarDataEntryFromMySql( const FaF::string & memcacheBarDataEntry )
    {
        const char expressionString [] = "expression";
    
        FaF::smatch stringResults;
        std::regex expression( expressionString );
        std::regex_match( memcacheBarDataEntry, stringResults, expression );
    
        ...
    }
    

    而且很管用。我想得太复杂了。。。

        2
  •  0
  •   Pixelchemist    8 年前

    根据 cppreference.com网站 ,的第二个参数 regex_match 应该是

    std::match_results<typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc>
    

    其中第一个参数是 std::basic_string<CharT,STraits,SAlloc> .

    这将需要在 FaF 喜欢有点像:

    namespace FaF
    {
        using string_type = std::basic_string<char, std::char_traits<char>,
                                Allocator<char>>;
        using match_type  = std::match_results<string_type::const_iterator, 
                                Allocator<string_type::const_iterator>>;
    }
    

    为了拥有

    • 通过分配器分配的字符串字符和
    • 正确的参数。
        3
  •  0
  •   Hybrid    5 年前

    FWIW,VS 2017无法编译match_type,因为它需要使用sub_match:

    using match_type = std::match_results<string_type::const_iterator,
        Allocator<std::sub_match<string_type::const_iterator>>>;
    

    编辑:实际上似乎不可能完全覆盖VS2017中std::regex使用的分配器。有些std:∶vector不带分配器参数,由_Matcher对象内部使用。这很烦人:(