对于std::string类,C++ length()方法是如何工作的?谢谢。
发布于 2011-04-11 07:11:38
它在大多数实现上都不起作用。它只是通过返回一个其他方法努力维护的值来欺骗:
来自libstdc++ string.h
/// Returns the number of characters in the string, not including any
/// null-termination.
size_type
length() const
{ return _M_rep()->_M_length; }顺便说一句,同样的事情也发生在Java中。显然,length()方法是习惯性的作弊手段。
https://stackoverflow.com/questions/5615239
复制相似问题