在哪里可以找到std::string::find()的完整实现
Where can I find the full implementation of std::string::find()?
我想知道为什么到目前为止我写的每个字符串搜索算法的实现都比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
相同的专业化,在大多数平台上应该与这两种类型之一相同的大小。
相关文章:
- 如果没有malloc,链表实现将失败
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 在libc++和libstdc++之间的std::map上使用std::find时的实现差异
- 如何在C++中从头文件中实现:find()和::count()
- 正在尝试在avl树中实现find
- 比较STL堆中std::shared_ptr和std::find的值.(试图实现A*)
- 自己的 std::string::find (暴力搜索)实现
- 在 LLVM libc++ 中找到的 string::find 中实现的算法(及其复杂性)是什么?
- 为什么std::find是这样实现的
- 在哪里可以找到std::string::find()的完整实现