小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

QByteArray參考

 職業(yè)醬油仔 2013-05-14
QByteArray類提供了一個字節(jié)數(shù)組,通常QByteArray被用來存儲了一般的字節(jié)(包括'\0')一級傳統(tǒng)的8位以'\0'結尾的字符串。由于QByteArray封裝的功能很多,使用起來比char*要方便的多,而就其內(nèi)部實現(xiàn)來講,它會保證所有的數(shù)據(jù)以'\0'結尾,使用隱式數(shù)據(jù)共享(copy-on-write)來減少內(nèi)存消耗以及不必要的數(shù)據(jù)拷貝。

除了QByteArray,Qt中還提供了QString類來存儲字符串,大部分情況下,我們都是在使用QString。QString存儲了16位unicode碼,很容易用來存儲非ASCII或是非Lantin1的編碼,另外QString在所有的QtAPI中都是通用的。有兩種情況下會比較適合使用QByteArray,第一就是你要存儲一般的位數(shù)據(jù),第二種情況就是在內(nèi)存資源很珍貴的情況下,例如像Qt for Embedded Linux
使用QByteArray很簡單,使用普通的字符串即可構造,如下所示:
  1. QByteArray ba("Hello");
復制代碼
上述代碼中ba的size()是5,但由于其在最后要存儲額外的'\0',其實際占用空間是6,這點我們要注意。像上面代碼一樣在使用字符串構造QByteArray時,QByteArray 執(zhí)行了深拷貝(deep copy),如果出于效率考慮你不想執(zhí)行深拷貝請使用QByteArray::fromRawData()。

跟C++的普通數(shù)組一樣,我們可以使用[]來訪問其具體下表對應的字節(jié),對于非const的QByteArray,我們可以像下面對具體的下表進行賦值:
  1. QByteArray ba;
  2. ba.resize(5);
  3. ba[0] = 0x3c;
  4. ba[1] = 0xb8;
  5. ba[2] = 0x64;
  6. ba[3] = 0x18;
  7. ba[4] = 0xca;
復制代碼
For read-only access, an alternative syntax is to use at():
對于只讀操作,請使用at(),因為它可以避免深拷貝,比使用[]要快,效率要高,就像下面這樣:
  1. for (int i = 0; i < ba.size(); ++i) {
  2.      if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
  3.          cout << "Found character in range [a-f]" << endl;
  4. }
復制代碼
如果一次取出多個字符,請使用left(), right(), 或者 mid()。
QByteArray里面可以放置的size()'\0'字節(jié)(注意這里不是最后結尾的'\0'),size()方法返回整個數(shù)組的長度,其中包括你加的那些'\0'字節(jié),如果想求的像char*一樣的長度請使用請使用qstrlen()
調用resize()方法之后,新增加的空間的值是不確定的,設置所有字節(jié)為某個值請使用fill()方法。

通過data() 或者 constData()可以獲得QByteArray的真實數(shù)據(jù)的指針,獲得的數(shù)據(jù)指針在調用QByteArray的non-const函數(shù)之前都是有效的

QByteArray提供了很多修改字節(jié)的方法: append(), prepend(), insert(), replace(), and remove()。如下所示

QByteArray x("and");
x.prepend("rock ");         // x == "rock and"
x.append(" roll");          // x == "rock and roll"
x.replace(5, 3, "&");       // x == "rock & roll"

The replace() and remove() functions' first two arguments are the position from which to start erasing and the number of bytes that should be erased.

When you append() data to a non-empty array, the array will be reallocated and the new data copied to it. You can avoid this behavior by calling reserve(), which preallocates a certain amount of memory. You can also call capacity() to find out how much memory QByteArray actually allocated. Data appended to an empty array is not copied.

A frequent requirement is to remove whitespace characters from a byte array ('\n', '\t', ' ', etc.). If you want to remove whitespace from both ends of a QByteArray, use trimmed(). If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the byte array, use simplified().

If you want to find all occurrences of a particular character or substring in a QByteArray, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if they find it; otherwise, they return -1. For example, here's a typical loop that finds all occurrences of a particular substring:

QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
int j = 0;
while ((j = ba.indexOf("<b>", j)) != -1) {
     cout << "Found <b> tag at index position " << j << endl;
     ++j;
}

If you simply want to check whether a QByteArray contains a particular character or substring, use contains(). If you want to find out how many times a particular character or substring occurs in the byte array, use count(). If you want to replace all occurrences of a particular value with another, use one of the two-parameter replace() overloads.

QByteArrays can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. The comparison is based exclusively on the numeric values of the characters and is very fast, but is not what a human would expect. QString::localeAwareCompare() is a better choice for sorting user-interface strings.

For historical reasons, QByteArray distinguishes between a null byte array and an empty byte array. A null byte array is a byte array that is initialized using QByteArray's default constructor or by passing (const char *)0 to the constructor. An empty byte array is any byte array with size 0. A null byte array is always empty, but an empty byte array isn't necessarily null:

QByteArray().isNull();          // returns true
QByteArray().isEmpty();         // returns true

QByteArray("").isNull();        // returns false
QByteArray("").isEmpty();       // returns true

QByteArray("abc").isNull();     // returns false
QByteArray("abc").isEmpty();    // returns false

All functions except isNull() treat null byte arrays the same as empty byte arrays. For example, data() returns a pointer to a '\0' character for a null byte array (not a null pointer), and QByteArray() compares equal to QByteArray(""). We recommend that you always use isEmpty() and avoid isNull().
Notes on Locale
Number-String Conversions

Functions that perform conversions between numeric data types and strings are performed in the C locale, irrespective of the user's locale settings. Use QString to perform locale-aware conversions between numbers and strings.
8-bit Character Comparisons

In QByteArray, the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions that support a case insensitive option or that compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains(), indexOf(), lastIndexOf(), operator<(), operator<=(), operator>(), operator>=(), toLower() and toUpper().

This issue does not apply to QStrings since they represent characters using Unicode.

See also QString and QBitArray.

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多