以前没注意length() count() size()这几个有什么区别,都是随意用,想到哪个就用哪个,但总是疑问到底他们有没有区别。
今天就去探个究竟,于是查qt的帮助文档,终于找到答案,三个确实没有区别,都是和size一样的!
以下均摘抄自qt帮助文档,希望可以给初学者提供帮助。
int QByteArray::length() const
Same as size().
This is an overloaded function.
Same as size().
Returns the number of bytes in this byte array.
The last byte in the byte array is at position size() - 1. In addition, QByteArray ensures that the byte at position size() is always '\0', so that you can use the return value of data() and constData() as arguments to functions that expect '\0'-terminated strings. If the QByteArray object was created from a raw data that didn't include the trailing null-termination character then QByteArray doesn't add it automaticall unless the deep copy is created.
Example:
QByteArray ba("Hello");
int n = ba.size(); // n == 5
ba.data()[0]; // returns 'H'
ba.data()[4]; // returns 'o'
ba.data()[5]; // returns '\0'
See also isEmpty() and resize().
希望可以帮到你们,有帮到你们的点个赞吧,你们的支持是我继续写博客的动力!