我希望消除算法中的特殊情况,为此我想确保std :: string :: npos + 1
等于 0
。
在我测试过的所有实现中都是正确的,并且在标准中搜索后,我发现了以下内容:
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
// ...
typedef typename allocator_traits<Allocator>::size_type size_type;
// ...
static const size_type npos = -1;
...
};
}
allocator_traits<Allocator>::size_type
被定义为一个无符号整数类型。
因此,我想知道,对于无符号整数类型T,(static_cast<T>(-1)) + 1
是否总是返回 0
?
numeric_limits<T>::max
是什么? - Neil Kirkstatic_assert (std::string::npos + 1 == 0, "bizarre string::npos")
语句。 - Basile Starynkevitcht = std::string()
的情况,否则返回s.substr(0, pos + 1)
。 - dottedmag