Boost C++ 库

……在全世界范围内,这是最受推崇、设计最精良的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

参考 - Boost C++ 函数库
PrevUpHomeNext

参考

string_algo 库的累积包含文件

定义了序列的大小写转换算法。算法使用提供的 locale 将输入序列的每个元素转换为所需的大小写。

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename RangeT> 
      OutputIteratorT 
      to_lower_copy(OutputIteratorT, const RangeT &, 
                    const std::locale & = std::locale());
    template<typename SequenceT> 
      SequenceT to_lower_copy(const SequenceT &, 
                              const std::locale & = std::locale());
    template<typename WritableRangeT> 
      void to_lower(WritableRangeT &, const std::locale & = std::locale());
    template<typename OutputIteratorT, typename RangeT> 
      OutputIteratorT 
      to_upper_copy(OutputIteratorT, const RangeT &, 
                    const std::locale & = std::locale());
    template<typename SequenceT> 
      SequenceT to_upper_copy(const SequenceT &, 
                              const std::locale & = std::locale());
    template<typename WritableRangeT> 
      void to_upper(WritableRangeT &, const std::locale & = std::locale());
  }
}

库中包含了分类谓词,以便在使用如 trim()all() 等算法时提供更多便利。它们将 STL 分类函数(例如 std::isspace())的功能封装成通用的函数对象。

namespace boost {
  namespace algorithm {
    unspecified is_classified(std::ctype_base::mask, 
                              const std::locale & = std::locale());
    unspecified is_space(const std::locale & = std::locale());
    unspecified is_alnum(const std::locale & = std::locale());
    unspecified is_alpha(const std::locale & = std::locale());
    unspecified is_cntrl(const std::locale & = std::locale());
    unspecified is_digit(const std::locale & = std::locale());
    unspecified is_graph(const std::locale & = std::locale());
    unspecified is_lower(const std::locale & = std::locale());
    unspecified is_print(const std::locale & = std::locale());
    unspecified is_punct(const std::locale & = std::locale());
    unspecified is_upper(const std::locale & = std::locale());
    unspecified is_xdigit(const std::locale & = std::locale());
    template<typename RangeT> unspecified is_any_of(const RangeT &);
    template<typename CharT> unspecified is_from_range(CharT, CharT);
    template<typename Pred1T, typename Pred2T> 
      unspecified operator&&(const predicate_facade< Pred1T > &, 
                             const predicate_facade< Pred2T > &);
    template<typename Pred1T, typename Pred2T> 
      unspecified operator||(const predicate_facade< Pred1T > &, 
                             const predicate_facade< Pred2T > &);
    template<typename PredT> 
      unspecified operator!(const predicate_facade< PredT > &);
  }
}

定义了元素比较谓词。此库中的许多算法可以接受一个额外的参数,该参数是一个用于比较元素的谓词。这使得例如实现算法的忽略大小写版本成为可能。

namespace boost {
  namespace algorithm {
    struct is_equal;
    struct is_iequal;
    struct is_iless;
    struct is_less;
    struct is_not_greater;
    struct is_not_igreater;
  }
}

定义了 string_algo 库中使用的概念

namespace boost {
  namespace algorithm {
    template<typename FinderT, typename IteratorT> struct FinderConcept;
    template<typename FormatterT, typename FinderT, typename IteratorT> 
      struct FormatterConcept;
  }
}
namespace boost {
  namespace algorithm {
    enum token_compress_mode_type;
  }
}

定义了各种擦除算法。每种算法根据搜索条件删除输入的一部分或多部分。

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename RangeT> 
      OutputIteratorT 
      erase_range_copy(OutputIteratorT, const RangeT &, 
                       const iterator_range< typename range_const_iterator< RangeT >::type > &);
    template<typename SequenceT> 
      SequenceT erase_range_copy(const SequenceT &, 
                                 const iterator_range< typename range_const_iterator< SequenceT >::type > &);
    template<typename SequenceT> 
      void erase_range(SequenceT &, 
                       const iterator_range< typename range_iterator< SequenceT >::type > &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      erase_first_copy(OutputIteratorT, const Range1T &, const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT erase_first_copy(const SequenceT &, const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void erase_first(SequenceT &, const RangeT &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      ierase_first_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                        const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      SequenceT ierase_first_copy(const SequenceT &, const RangeT &, 
                                  const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      void ierase_first(SequenceT &, const RangeT &, 
                        const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      erase_last_copy(OutputIteratorT, const Range1T &, const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT erase_last_copy(const SequenceT &, const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void erase_last(SequenceT &, const RangeT &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      ierase_last_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                       const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      SequenceT ierase_last_copy(const SequenceT &, const RangeT &, 
                                 const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      void ierase_last(SequenceT &, const RangeT &, 
                       const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      erase_nth_copy(OutputIteratorT, const Range1T &, const Range2T &, int);
    template<typename SequenceT, typename RangeT> 
      SequenceT erase_nth_copy(const SequenceT &, const RangeT &, int);
    template<typename SequenceT, typename RangeT> 
      void erase_nth(SequenceT &, const RangeT &, int);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      ierase_nth_copy(OutputIteratorT, const Range1T &, const Range2T &, int, 
                      const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      SequenceT ierase_nth_copy(const SequenceT &, const RangeT &, int, 
                                const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      void ierase_nth(SequenceT &, const RangeT &, int, 
                      const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      erase_all_copy(OutputIteratorT, const Range1T &, const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT erase_all_copy(const SequenceT &, const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void erase_all(SequenceT &, const RangeT &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      ierase_all_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                      const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      SequenceT ierase_all_copy(const SequenceT &, const RangeT &, 
                                const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      void ierase_all(SequenceT &, const RangeT &, 
                      const std::locale & = std::locale());
    template<typename OutputIteratorT, typename RangeT> 
      OutputIteratorT erase_head_copy(OutputIteratorT, const RangeT &, int);
    template<typename SequenceT> 
      SequenceT erase_head_copy(const SequenceT &, int);
    template<typename SequenceT> void erase_head(SequenceT &, int);
    template<typename OutputIteratorT, typename RangeT> 
      OutputIteratorT erase_tail_copy(OutputIteratorT, const RangeT &, int);
    template<typename SequenceT> 
      SequenceT erase_tail_copy(const SequenceT &, int);
    template<typename SequenceT> void erase_tail(SequenceT &, int);
  }
}

定义了一组查找算法。这些算法用于搜索输入字符串中的子字符串。结果以 iterator_range 的形式给出,用于界定子字符串。

namespace boost {
  namespace algorithm {
    template<typename RangeT, typename FinderT> 
      iterator_range< typename range_iterator< RangeT >::type > 
      find(RangeT &, const FinderT &);
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      find_first(Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      ifind_first(Range1T &, const Range2T &, 
                  const std::locale & = std::locale());
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      find_last(Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      ifind_last(Range1T &, const Range2T &, 
                 const std::locale & = std::locale());
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      find_nth(Range1T &, const Range2T &, int);
    template<typename Range1T, typename Range2T> 
      iterator_range< typename range_iterator< Range1T >::type > 
      ifind_nth(Range1T &, const Range2T &, int, 
                const std::locale & = std::locale());
    template<typename RangeT> 
      iterator_range< typename range_iterator< RangeT >::type > 
      find_head(RangeT &, int);
    template<typename RangeT> 
      iterator_range< typename range_iterator< RangeT >::type > 
      find_tail(RangeT &, int);
    template<typename RangeT, typename PredicateT> 
      iterator_range< typename range_iterator< RangeT >::type > 
      find_token(RangeT &, PredicateT, 
                 token_compress_mode_type = token_compress_off);
  }
}

定义了通用的替换算法。每种算法都会替换输入的全部或部分内容。要替换的部分通过 Finder 对象查找。查找结果随后由 Formatter 对象用于生成替换内容。

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename RangeT, typename FinderT, 
             typename FormatterT> 
      OutputIteratorT 
      find_format_copy(OutputIteratorT, const RangeT &, FinderT, FormatterT);
    template<typename SequenceT, typename FinderT, typename FormatterT> 
      SequenceT find_format_copy(const SequenceT &, FinderT, FormatterT);
    template<typename SequenceT, typename FinderT, typename FormatterT> 
      void find_format(SequenceT &, FinderT, FormatterT);
    template<typename OutputIteratorT, typename RangeT, typename FinderT, 
             typename FormatterT> 
      OutputIteratorT 
      find_format_all_copy(OutputIteratorT, const RangeT &, FinderT, 
                           FormatterT);
    template<typename SequenceT, typename FinderT, typename FormatterT> 
      SequenceT find_format_all_copy(const SequenceT &, FinderT, FormatterT);
    template<typename SequenceT, typename FinderT, typename FormatterT> 
      void find_format_all(SequenceT &, FinderT, FormatterT);
  }
}

定义了查找迭代器类。查找迭代器会反复应用 Finder 对指定的输入字符串进行搜索匹配。解引用迭代器会根据所使用的迭代器产生当前匹配项或上次匹配项与当前匹配项之间的范围。

namespace boost {
  namespace algorithm {
    template<typename IteratorT> class find_iterator;
    template<typename IteratorT> class split_iterator;
    template<typename RangeT, typename FinderT> 
      find_iterator< typename range_iterator< RangeT >::type > 
      make_find_iterator(RangeT &, FinderT);
    template<typename RangeT, typename FinderT> 
      split_iterator< typename range_iterator< RangeT >::type > 
      make_split_iterator(RangeT &, FinderT);
  }
}

定义了 Finder 生成器。Finder 对象是一个函数对象,它能够根据特定条件在输入中查找匹配的子字符串。Finder 用作替换、查找和分割功能的插件组件。此头文件包含本库提供的 Finder 的生成函数。

namespace boost {
  namespace algorithm {
    template<typename RangeT> unspecified first_finder(const RangeT &);
    template<typename RangeT, typename PredicateT> 
      unspecified first_finder(const RangeT &, PredicateT);
    template<typename RangeT> unspecified last_finder(const RangeT &);
    template<typename RangeT, typename PredicateT> 
      unspecified last_finder(const RangeT &, PredicateT);
    template<typename RangeT> unspecified nth_finder(const RangeT &, int);
    template<typename RangeT, typename PredicateT> 
      unspecified nth_finder(const RangeT &, int, PredicateT);
    unspecified head_finder(int);
    unspecified tail_finder(int);
    template<typename PredicateT> 
      unspecified token_finder(PredicateT, 
                               token_compress_mode_type = token_compress_off);
    template<typename ForwardIteratorT> 
      unspecified range_finder(ForwardIteratorT, ForwardIteratorT);
    template<typename ForwardIteratorT> 
      unspecified range_finder(iterator_range< ForwardIteratorT >);
  }
}

定义了 Formatter 生成器。Formatter 是一个根据给定参数格式化字符串的函数对象。Formatter 与 Finder 协同工作。Finder 可以为特定的 Formatter 提供额外信息。这种合作的一个例子是 regex_finder 和 regex_formatter。

Formatter 用作替换功能的插件组件。此头文件包含本库提供的 Formatter 的生成函数。

namespace boost {
  namespace algorithm {
    template<typename RangeT> unspecified const_formatter(const RangeT &);
    template<typename RangeT> unspecified identity_formatter();
    template<typename RangeT> unspecified empty_formatter(const RangeT &);
    template<typename FinderT> unspecified dissect_formatter(const FinderT &);
  }
}

定义了通用的分割算法。分割算法可用于根据给定条件将序列分割成多个部分。结果以“容器的容器”的形式给出,其中元素是提取部分的副本或引用。

提供了两种算法。一种遍历匹配的子字符串,另一种遍历这些匹配项之间的间隔。

namespace boost {
  namespace algorithm {
    template<typename SequenceSequenceT, typename RangeT, typename FinderT> 
      SequenceSequenceT & iter_find(SequenceSequenceT &, RangeT &&, FinderT);
    template<typename SequenceSequenceT, typename RangeT, typename FinderT> 
      SequenceSequenceT & iter_split(SequenceSequenceT &, RangeT &&, FinderT);
  }
}

定义了 join 算法。

join 算法是 split 算法的对应操作。它通过添加用户定义的*分隔符*来连接“列表”中的字符串。此外,还有一个版本允许通过提供谓词进行简单的过滤。

namespace boost {
  namespace algorithm {
    template<typename SequenceSequenceT, typename Range1T> 
      range_value< SequenceSequenceT >::type 
      join(const SequenceSequenceT &, const Range1T &);
    template<typename SequenceSequenceT, typename Range1T, 
             typename PredicateT> 
      range_value< SequenceSequenceT >::type 
      join_if(const SequenceSequenceT &, const Range1T &, PredicateT);
  }
}

定义了与字符串相关的谓词。这些谓词在各种条件下确定子字符串是否包含在输入字符串中:字符串以子字符串开头、以子字符串结尾、简单包含子字符串或两者字符串相等。此外,算法 all() 检查容器的所有元素是否满足某个条件。

所有谓词都提供强大的异常保证。

namespace boost {
  namespace algorithm {
    template<typename Range1T, typename Range2T, typename PredicateT> 
      bool starts_with(const Range1T &, const Range2T &, PredicateT);
    template<typename Range1T, typename Range2T> 
      bool starts_with(const Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      bool istarts_with(const Range1T &, const Range2T &, 
                        const std::locale & = std::locale());
    template<typename Range1T, typename Range2T, typename PredicateT> 
      bool ends_with(const Range1T &, const Range2T &, PredicateT);
    template<typename Range1T, typename Range2T> 
      bool ends_with(const Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      bool iends_with(const Range1T &, const Range2T &, 
                      const std::locale & = std::locale());
    template<typename Range1T, typename Range2T, typename PredicateT> 
      bool contains(const Range1T &, const Range2T &, PredicateT);
    template<typename Range1T, typename Range2T> 
      bool contains(const Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      bool icontains(const Range1T &, const Range2T &, 
                     const std::locale & = std::locale());
    template<typename Range1T, typename Range2T, typename PredicateT> 
      bool equals(const Range1T &, const Range2T &, PredicateT);
    template<typename Range1T, typename Range2T> 
      bool equals(const Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      bool iequals(const Range1T &, const Range2T &, 
                   const std::locale & = std::locale());
    template<typename Range1T, typename Range2T, typename PredicateT> 
      bool lexicographical_compare(const Range1T &, const Range2T &, 
                                   PredicateT);
    template<typename Range1T, typename Range2T> 
      bool lexicographical_compare(const Range1T &, const Range2T &);
    template<typename Range1T, typename Range2T> 
      bool ilexicographical_compare(const Range1T &, const Range2T &, 
                                    const std::locale & = std::locale());
    template<typename RangeT, typename PredicateT> 
      bool all(const RangeT &, PredicateT);
  }
}

定义了算法的正则表达式变体。

namespace boost {
  namespace algorithm {
    template<typename RangeT, typename CharT, typename RegexTraitsT> 
      iterator_range< typename range_iterator< RangeT >::type > 
      find_regex(RangeT &, const basic_regex< CharT, RegexTraitsT > &, 
                 match_flag_type = match_default);
    template<typename OutputIteratorT, typename RangeT, typename CharT, 
             typename RegexTraitsT, typename FormatStringTraitsT, 
             typename FormatStringAllocatorT> 
      OutputIteratorT 
      replace_regex_copy(OutputIteratorT, const RangeT &, 
                         const basic_regex< CharT, RegexTraitsT > &, 
                         const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                         match_flag_type = match_default|format_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT, 
             typename FormatStringTraitsT, typename FormatStringAllocatorT> 
      SequenceT replace_regex_copy(const SequenceT &, 
                                   const basic_regex< CharT, RegexTraitsT > &, 
                                   const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                                   match_flag_type = match_default|format_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT, 
             typename FormatStringTraitsT, typename FormatStringAllocatorT> 
      void replace_regex(SequenceT &, 
                         const basic_regex< CharT, RegexTraitsT > &, 
                         const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                         match_flag_type = match_default|format_default);
    template<typename OutputIteratorT, typename RangeT, typename CharT, 
             typename RegexTraitsT, typename FormatStringTraitsT, 
             typename FormatStringAllocatorT> 
      OutputIteratorT 
      replace_all_regex_copy(OutputIteratorT, const RangeT &, 
                             const basic_regex< CharT, RegexTraitsT > &, 
                             const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                             match_flag_type = match_default|format_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT, 
             typename FormatStringTraitsT, typename FormatStringAllocatorT> 
      SequenceT replace_all_regex_copy(const SequenceT &, 
                                       const basic_regex< CharT, RegexTraitsT > &, 
                                       const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                                       match_flag_type = match_default|format_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT, 
             typename FormatStringTraitsT, typename FormatStringAllocatorT> 
      void replace_all_regex(SequenceT &, 
                             const basic_regex< CharT, RegexTraitsT > &, 
                             const std::basic_string< CharT, FormatStringTraitsT, FormatStringAllocatorT > &, 
                             match_flag_type = match_default|format_default);
    template<typename OutputIteratorT, typename RangeT, typename CharT, 
             typename RegexTraitsT> 
      OutputIteratorT 
      erase_regex_copy(OutputIteratorT, const RangeT &, 
                       const basic_regex< CharT, RegexTraitsT > &, 
                       match_flag_type = match_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT> 
      SequenceT erase_regex_copy(const SequenceT &, 
                                 const basic_regex< CharT, RegexTraitsT > &, 
                                 match_flag_type = match_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT> 
      void erase_regex(SequenceT &, 
                       const basic_regex< CharT, RegexTraitsT > &, 
                       match_flag_type = match_default);
    template<typename OutputIteratorT, typename RangeT, typename CharT, 
             typename RegexTraitsT> 
      OutputIteratorT 
      erase_all_regex_copy(OutputIteratorT, const RangeT &, 
                           const basic_regex< CharT, RegexTraitsT > &, 
                           match_flag_type = match_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT> 
      SequenceT erase_all_regex_copy(const SequenceT &, 
                                     const basic_regex< CharT, RegexTraitsT > &, 
                                     match_flag_type = match_default);
    template<typename SequenceT, typename CharT, typename RegexTraitsT> 
      void erase_all_regex(SequenceT &, 
                           const basic_regex< CharT, RegexTraitsT > &, 
                           match_flag_type = match_default);
    template<typename SequenceSequenceT, typename RangeT, typename CharT, 
             typename RegexTraitsT> 
      SequenceSequenceT & 
      find_all_regex(SequenceSequenceT &, const RangeT &, 
                     const basic_regex< CharT, RegexTraitsT > &, 
                     match_flag_type = match_default);
    template<typename SequenceSequenceT, typename RangeT, typename CharT, 
             typename RegexTraitsT> 
      SequenceSequenceT & 
      split_regex(SequenceSequenceT &, const RangeT &, 
                  const basic_regex< CharT, RegexTraitsT > &, 
                  match_flag_type = match_default);
    template<typename SequenceSequenceT, typename Range1T, typename CharT, 
             typename RegexTraitsT> 
      range_value< SequenceSequenceT >::type 
      join_if(const SequenceSequenceT &, const Range1T &, 
              const basic_regex< CharT, RegexTraitsT > &, 
              match_flag_type = match_default);
  }
}

定义了 regex_finderregex_formatter 生成器。这两个函数对象设计为协同工作。regex_formatter 使用 regex_finder 搜索结果中包含的匹配项的额外信息。

namespace boost {
  namespace algorithm {
    template<typename CharT, typename RegexTraitsT> 
      unspecified regex_finder(const basic_regex< CharT, RegexTraitsT > &, 
                               match_flag_type = match_default);
    template<typename CharT, typename TraitsT, typename AllocT> 
      unspecified regex_formatter(const std::basic_string< CharT, TraitsT, AllocT > &, 
                                  match_flag_type = format_default);
  }
}

定义了各种替换算法。每种算法根据一组搜索和替换标准替换输入的部分或全部内容。

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      replace_range_copy(OutputIteratorT, const Range1T &, 
                         const iterator_range< typename range_const_iterator< Range1T >::type > &, 
                         const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT replace_range_copy(const SequenceT &, 
                                   const iterator_range< typename range_const_iterator< SequenceT >::type > &, 
                                   const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void replace_range(SequenceT &, 
                         const iterator_range< typename range_iterator< SequenceT >::type > &, 
                         const RangeT &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      replace_first_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                         const Range3T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT replace_first_copy(const SequenceT &, const Range1T &, 
                                   const Range2T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void replace_first(SequenceT &, const Range1T &, const Range2T &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      ireplace_first_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                          const Range3T &, 
                          const std::locale & = std::locale());
    template<typename SequenceT, typename Range2T, typename Range1T> 
      SequenceT ireplace_first_copy(const SequenceT &, const Range2T &, 
                                    const Range1T &, 
                                    const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void ireplace_first(SequenceT &, const Range1T &, const Range2T &, 
                          const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      replace_last_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                        const Range3T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT replace_last_copy(const SequenceT &, const Range1T &, 
                                  const Range2T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void replace_last(SequenceT &, const Range1T &, const Range2T &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      ireplace_last_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                         const Range3T &, 
                         const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT ireplace_last_copy(const SequenceT &, const Range1T &, 
                                   const Range2T &, 
                                   const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void ireplace_last(SequenceT &, const Range1T &, const Range2T &, 
                         const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      replace_nth_copy(OutputIteratorT, const Range1T &, const Range2T &, int, 
                       const Range3T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT replace_nth_copy(const SequenceT &, const Range1T &, int, 
                                 const Range2T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void replace_nth(SequenceT &, const Range1T &, int, const Range2T &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      ireplace_nth_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                        int, const Range3T &, 
                        const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT ireplace_nth_copy(const SequenceT &, const Range1T &, int, 
                                  const Range2T &, 
                                  const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void ireplace_nth(SequenceT &, const Range1T &, int, const Range2T &, 
                        const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      replace_all_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                       const Range3T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT replace_all_copy(const SequenceT &, const Range1T &, 
                                 const Range2T &);
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void replace_all(SequenceT &, const Range1T &, const Range2T &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T, 
             typename Range3T> 
      OutputIteratorT 
      ireplace_all_copy(OutputIteratorT, const Range1T &, const Range2T &, 
                        const Range3T &, const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      SequenceT ireplace_all_copy(const SequenceT &, const Range1T &, 
                                  const Range2T &, 
                                  const std::locale & = std::locale());
    template<typename SequenceT, typename Range1T, typename Range2T> 
      void ireplace_all(SequenceT &, const Range1T &, const Range2T &, 
                        const std::locale & = std::locale());
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      replace_head_copy(OutputIteratorT, const Range1T &, int, 
                        const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT replace_head_copy(const SequenceT &, int, const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void replace_head(SequenceT &, int, const RangeT &);
    template<typename OutputIteratorT, typename Range1T, typename Range2T> 
      OutputIteratorT 
      replace_tail_copy(OutputIteratorT, const Range1T &, int, 
                        const Range2T &);
    template<typename SequenceT, typename RangeT> 
      SequenceT replace_tail_copy(const SequenceT &, int, const RangeT &);
    template<typename SequenceT, typename RangeT> 
      void replace_tail(SequenceT &, int, const RangeT &);
  }
}

此头文件中定义的特性被各种算法用来为特定容器实现更好的性能。特性提供了安全默认值。如果容器支持其中某些功能,则可以为该特定容器特化特定的特性。对于不兼容的编译器,可以为特定的测试函数定义覆盖。

由于语言限制,目前无法在不包含相应头文件的情况下为 stl 容器定义特化。为了减少此包含所需的开销,用户可以有选择地为特定容器包含特化头文件。它们位于 boost/algorithm/string/stl 目录中。或者,她可以包含 boost/algorithm/string/std_collection_traits.hpp 头文件,其中包含所有 stl 容器的特化。

namespace boost {
  namespace algorithm {
    template<typename T> class has_const_time_erase;
    template<typename T> class has_const_time_insert;
    template<typename T> class has_native_replace;
    template<typename T> class has_stable_iterators;
  }
}

定义了基本的分割算法。分割算法可用于根据给定条件将字符串分成几个部分。

每个部分都会被复制并作为新元素添加到输出容器中。因此,结果容器必须能够容纳匹配项的副本(以兼容的结构,如 std::string)或其引用(例如,使用迭代器范围类)。此类容器的示例是 std::vector<std::string>std::list<boost::iterator_range<std::string::iterator>>

namespace boost {
  namespace algorithm {
    template<typename SequenceSequenceT, typename Range1T, typename Range2T> 
      SequenceSequenceT & 
      find_all(SequenceSequenceT &, Range1T &&, const Range2T &);
    template<typename SequenceSequenceT, typename Range1T, typename Range2T> 
      SequenceSequenceT & 
      ifind_all(SequenceSequenceT &, Range1T &&, const Range2T &, 
                const std::locale & = std::locale());
    template<typename SequenceSequenceT, typename RangeT, typename PredicateT> 
      SequenceSequenceT & 
      split(SequenceSequenceT &, RangeT &&, PredicateT, 
            token_compress_mode_type = token_compress_off);
  }
}

此文件包含 stl 容器的序列特性。

定义了 trim 算法。trim 算法用于从序列(字符串)中删除前导和尾随空格。空格是使用给定的 locale 来识别的。

参数化(_if)变体使用谓词(函数对象)来选择要修剪的字符。函数接受一个选择谓词作为参数,用于确定一个字符是否为空格。常用谓词在 classification.hpp 头文件中提供。

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename RangeT, typename PredicateT> 
      OutputIteratorT 
      trim_left_copy_if(OutputIteratorT, const RangeT &, PredicateT);
    template<typename SequenceT, typename PredicateT> 
      SequenceT trim_left_copy_if(const SequenceT &, PredicateT);
    template<typename SequenceT> 
      SequenceT trim_left_copy(const SequenceT &, 
                               const std::locale & = std::locale());
    template<typename SequenceT, typename PredicateT> 
      void trim_left_if(SequenceT &, PredicateT);
    template<typename SequenceT> 
      void trim_left(SequenceT &, const std::locale & = std::locale());
    template<typename OutputIteratorT, typename RangeT, typename PredicateT> 
      OutputIteratorT 
      trim_right_copy_if(OutputIteratorT, const RangeT &, PredicateT);
    template<typename SequenceT, typename PredicateT> 
      SequenceT trim_right_copy_if(const SequenceT &, PredicateT);
    template<typename SequenceT> 
      SequenceT trim_right_copy(const SequenceT &, 
                                const std::locale & = std::locale());
    template<typename SequenceT, typename PredicateT> 
      void trim_right_if(SequenceT &, PredicateT);
    template<typename SequenceT> 
      void trim_right(SequenceT &, const std::locale & = std::locale());
    template<typename OutputIteratorT, typename RangeT, typename PredicateT> 
      OutputIteratorT 
      trim_copy_if(OutputIteratorT, const RangeT &, PredicateT);
    template<typename SequenceT, typename PredicateT> 
      SequenceT trim_copy_if(const SequenceT &, PredicateT);
    template<typename SequenceT> 
      SequenceT trim_copy(const SequenceT &, 
                          const std::locale & = std::locale());
    template<typename SequenceT, typename PredicateT> 
      void trim_if(SequenceT &, PredicateT);
    template<typename SequenceT> 
      void trim(SequenceT &, const std::locale & = std::locale());
  }
}

定义了 trim_all 算法。

trim 类似,trim_all 会删除序列(字符串)中的所有前导和尾随空格。此外,序列中间的空格会被截断为单个字符。空格是使用给定的 locale 来识别的。

trim_fill 的作用与 trim_all 类似,但中间的空格会被用户定义的字符序列替换。

参数化(_if)变体使用谓词(函数对象)来选择要修剪的字符。函数接受一个选择谓词作为参数,用于确定一个字符是否为空格。常用谓词在 classification.hpp 头文件中提供。

namespace boost {
  namespace algorithm {
    template<typename SequenceT, typename PredicateT> 
      SequenceT trim_all_copy_if(const SequenceT &, PredicateT);
    template<typename SequenceT, typename PredicateT> 
      void trim_all_if(SequenceT &, PredicateT);
    template<typename SequenceT> 
      SequenceT trim_all_copy(const SequenceT &, 
                              const std::locale & = std::locale());
    template<typename SequenceT> 
      void trim_all(SequenceT &, const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT, typename PredicateT> 
      SequenceT trim_fill_copy_if(const SequenceT &, const RangeT &, 
                                  PredicateT);
    template<typename SequenceT, typename RangeT, typename PredicateT> 
      void trim_fill_if(SequenceT &, const RangeT &, PredicateT);
    template<typename SequenceT, typename RangeT> 
      SequenceT trim_fill_copy(const SequenceT &, const RangeT &, 
                               const std::locale & = std::locale());
    template<typename SequenceT, typename RangeT> 
      void trim_fill(SequenceT &, const RangeT &, 
                     const std::locale & = std::locale());
  }
}

string_algo 库的累积包含文件。除了 string.hpp 之外,还包含正则表达式相关的代码。


PrevUpHomeNext