#177·MyTinySTL

basic_string.h里面的compare(size_type pos1, size_type count1, const basic_string& other,size_type pos2, size_type count2) const逻辑出问题

Author: terrfgCreated Mar 27, 2025Updated Mar 27, 2025

// 从 pos1 下标开始的 count1 个字符跟另一个 basic_string 下标 pos2 开始的 count2 个字符比较 template <class CharType, class CharTraits> int basic_string<CharType, CharTraits>:: compare(size_type pos1, size_type count1, const basic_string& other, size_type pos2, size_type count2) const { auto n1 = mystl::min(count1, size_ - pos1); auto n2 = mystl::min(count2, other.size_ - pos2); return compare_cstr(buffer_, n1, other.buffer_, n2); } 这里的compare_cstr(buffer_, n1, other.buffer_, n2); 应该是compare_cstr(buffer_+pos1, n1, other.buffer_+pos2, n2);