C++ 标准库中缺少的字符串函数:`starts_with` 和 `ends_with`
C++ 标准模板库的 std::string 很好很强大,但是并没有提供判断一个字符串是否以另一个字符串开始/结束的接口。这里为 std::basic_string<charT> 提供这两个接口。 //string_predicate.hpp #include <string> namespace std { template <typename charT> inline bool starts_with(const basic_string<charT>& big, const basic_string<charT>& small) { if (&big == &small) return true; const typename basic_string<charT>::size_type big_size = big.size(); const typename basic_string<charT>::size_type small_size = small.size(); const bool valid_ = (big_size >= small_size); const bool starts_with_ = (big.compare(0, small_size, small) == 0); return valid_ and starts_with_; } template <typename charT> inline bool ends_with(const basic_string<charT>& big, const basic_string<charT>& small) { if (&big == &small) return true; const typename basic_string<charT>::size_type big_size = big.size(); const……