在哪里可以找到std::string::find()的完整实现

Where can I find the full implementation of std::string::find()?

本文关键字:实现 find string std 在哪里      更新时间:2023-10-16

我想知道为什么到目前为止我写的每个字符串搜索算法的实现都比std::string提供的算法差得多。

不仅更好,而且无论我是否在线程之间分配负载,它似乎都能在恒定时间内执行(这里的恒定是指它的变化很少取决于字符串和/或子字符串的大小)。

这是我的编译器:

g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

到目前为止,我已经设法挖掘了位于/usr/include/c++/4.8.1/bits中的basic_string.tcc,这为std::string::find产生了这个:

template<typename _CharT, typename _Traits, typename _Alloc>
    typename basic_string<_CharT, _Traits, _Alloc>::size_type
    basic_string<_CharT, _Traits, _Alloc>::
    find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
    {
        size_type __ret = npos;
        const size_type __size = this->size();
            if (__pos < __size)
            {
                const _CharT* __data = _M_data();
                const size_type __n = __size - __pos;
                const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
                if (__p)
                __ret = __p - __data;
            }
        return __ret;
    }

我现在唯一缺少的是这一行:

const _CharT* __p = traits_type::find(__data + __pos, __n, __c);

有没有人知道我在哪里可以找到traits_type::find的实现?

我发现:

  • 是通用的,
  • 一个用于char
  • 一个用于wchar_t
  • 一个用于char16_t
  • 和一个char32_t

char/wchar_t只是调用C(内置)函数,我注意到现在他们可以优化char16_t/char32_t中的一个,使用与wchar_t相同的专业化,在大多数平台上应该与这两种类型之一相同的大小。