【问题标题】:c++ compile time constructed binary search treec++编译时构造二叉搜索树
【发布时间】:2014-01-07 23:55:10
【问题描述】:

我希望能够说trim<' ', '\t', '\n'>(str),让它在下面使用is_ws<' ', '\t', '\n'>(ch) 作为谓词。我希望谓词对每个ch 进行 O(log(N)) 比较。不编译(模糊签名)O(N)片段来演示这个想法:

template <char W1, char...Wn> bool is_ws(char ch) {
    return is_ws<W1>(ch) || is_ws<Wn...>(ch);
}

template <char W> bool is_ws(char ch) {
    return W == ch;
}

如何做到这一点?

【问题讨论】:

    标签: c++ c++11 binary-search-tree template-meta-programming


    【解决方案1】:

    我做了完全相同的事情,除了我作弊并使用了一个位域(32位),但我确实从模板参数编译时组装了位域:

    template <char...> struct CtrlVec ;
    template <char c> struct CtrlVec<c>
        { static_assert( ( c < 0x20 ), "Only for control characters" ) ;  enum { mask = ( 1 << c ) } ; } ;
    template <char c, char... cs> struct CtrlVec<c, cs...>
        { static_assert( ( c < 0x20 ), "Only for control characters" ) ;  enum { mask = ( 1 << c) | CtrlVec<cs...>::mask } ; } ;
    

    如果您对搜索一无所知,我将创建一个 char "\t\n " 的排序数组并使用 std::lower_bound 进行搜索。我还会对数组元素执行static_assert 以确认其正确构造。

    【讨论】:

    • std::is_sorted() constexpr?
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多