QString Detailed study of QT

QString Detailed study of QT


QString class holds 16-bit Unicode values, provides a wealth of operations, query and conversion functions. The class also made use implicitly shared , efficient memory allocation strategy multifaceted optimization.

A string common operations

String operations :

function Explanation
str.append(“xxx”) At the end of a string append another string
str.sprintf("%s",“HelloWorld”) Assigned to the string HelloWorld
str=QString("%1 is good").arg(“Bike”) arg () function after occupying fill
str.insert(1,“Hello”) Insert "Hello" in the second character string
str.prepend(“Hello”) Another string is inserted at the beginning of the source string
str.replace() Some string in place of the original string with the specified string
QString::trimmed() Remove both ends of the string of blank characters
QString::simplified() Removal of whitespace character string ends, a single blank character "" instead of the character string appearing white

String query :

function Explanation
str.startWith ( '@', Qt :: Case Sensitive) Whether beginning with a specific character
str.endWith(’#’,Qt::CaseSensitive) Whether the end of a specific character
str.indexof(’*’,Qt::CaseSensitive) It returns the index of a specific character string
str.contains(‘Welcome’,Qt::CaseSensitive) The query string contains a specific string

String conversion :

function Explanation
str.toInt (& ok, 16) // 16 to hex, the default is 10 Converting the string to an integer value
str.toAscii () Returns a string of ASCII coded 8-bit
str.Latin1() Returns a Latin-1 8-bit coded character string
str.toUtf8 () // UTF-8 is a superset of the ASCII code Returns a UTF-8 encoding of the string 8
str.toLocal8Bit() The system returns a string of 8-bit encoding native

The difference between the NULL string and an empty string : a NULL string is used QString default constructor or by using "(const char *) 0" QString constructor argument as a String object created; and a is an empty string a string of size 0. A NULL string must be an empty string, while an empty string is not a NULL string.

Second, the shared string implicit

Implicit sharing, also known as write-back copy. When two objects share the same data (the data block is shared by a shallow copy), if the data is not changed, the data is not copied. When changing the data for an object, a deep copy is performed.

While processing the shared object, using a deep and shallow copy copy copy objects two methods. The so-called deep copy refers to a complete replica generating objects, perform a deep copy of the cost is more expensive, take up more memory and CPU resources; shallow copy of efficiency is very high, it is only a set pointer to the shared data block and the modified reference count value.

Implicit sharing can reduce the usage of memory and CPU resources, improve operational efficiency of the program. It makes more efficient use of the transmission function values ​​(e.g., parameters, return values) of the.

QString class implicit share count, the deep and shallow copy copy mailing combine be described below through an example.

QString str1="data";
QString str2=str1;      //(a)
str2[3]='e';            //(b)
str2[0]='f';            //(c)
str1=str2;              //(d)

(A) = QString str2 str1 : the assignment to another String object str1 string str2 (performed by the copy constructor QString str2 initialization), this time, str2 = "data". When assignment of str2, a shallow copy will occur, resulting in two QString objects point to the same data structure. The data structure stored in addition to the string "data", but also holds a reference counter to record the number of the reference character string data. Here, because str1 and str2 point to the same data structure, all the counter is 2.

(B) str2 [. 3] = 'E' : QString objects str2 modifications will lead to a deep copy, so that a new object points str2, str1 different from the data structure within the meaning of (the reference count of the data structure 1 , because only str2 point to the data structure), and modify the original data structure str1 point is provided on its reference count is 1 (in this case, only the object str1 points QString the data structure). Chicken children in this str2 point to complete the modification of the data on the new data structure. A reference count of 1 means that the data is not shared. At this time, str2 = "date", str1 = "data".

(C) str2 [0] = 'F' : QString objects str2 further modified, but this action will not cause any kind of copying, since the data structure is not shared str2 point. At this time, str2 = "fate", str1 = "data".

(D) = str2 str1 : str2 assigned to the str1. In this case, it points to a reference str1 the data structure of a modified counter value is 0, i.e., no longer use QString objects of the data structure. Therefore, str1 will point to data structures freed from memory. The result of this is that, subject QString str1 and str2 point string "fate" of a data structure, the data structure of the reference count is 2.

Qt support implicitly shared class, further comprising:

  1. All container classes.
  2. QByteArray、QBrush、QPen、QPalette、QBitmap、QImage、QPixmap、QCursor、QDir、QFont和QVariant等。

Third, the string of memory allocation strategy

QString string data stored in a contiguous block of memory. When the length of the string continued growth, QString need to re-allocate memory space, so that there is sufficient controls to save additional strings. QString memory allocation strategy are as follows:

  1. 4 characters each allocation space, until the size of 20.
  2. Between 20 and 4084, the allocated memory block size QString 2 times the rate of growth.
  3. From the beginning 4084, 2048 to each character size (4096 bytes, i.e., 4KB) a growth step.

The following example illustrates how QString run in the background:

QString test()
{
    QString str;
    for(int i=0;i<9000;++i)
        str.append("a");
    
    return str;
}

Description: First define a QString objects str, then it is appended to 9000 characters. According to QString's memory allocation strategy. This cycle of operation will result in redistribution of memory 14: 4,8,16,20,52,116,244,500,1012,2036,4084,6132,8180,10228. After the last memory reallocation operation, str QString objects having memory blocks (20456 bytes) a 10,228 Unicode character size, space characters which are used 9000 (18,000 bytes).

Published 48 original articles · won praise 5 · Views 2591

Guess you like

Origin blog.csdn.net/Mr_robot_strange/article/details/104609521