QBitArray 類提供位數組。 更多...
| 頭: |
#include <QBitArray>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
此類 相等可比較 .
注意: 此類的所有函數 可重入 .
| QBitArray () | |
| QBitArray (qsizetype size , bool value = false) | |
| QBitArray (const QBitArray & other ) | |
| QBitArray (QBitArray && other ) | |
| bool | at (qsizetype i ) const |
| const char * | bits () const |
| void | clear () |
| void | clearBit (qsizetype i ) |
| qsizetype | count () const |
| qsizetype | count (bool on ) const |
| bool | fill (bool value , qsizetype size = -1) |
| void | fill (bool value , qsizetype begin , qsizetype end ) |
| bool | isEmpty () const |
| bool | isNull () const |
| void | resize (qsizetype size ) |
| void | setBit (qsizetype i ) |
| void | setBit (qsizetype i , bool value ) |
| qsizetype | size () const |
| void | swap (QBitArray & other ) |
| bool | testBit (qsizetype i ) const |
(從 6.0 起)
quint32
|
toUInt32 (QSysInfo::Endian endianness , bool * ok = nullptr) const |
| bool | toggleBit (qsizetype i ) |
| void | truncate (qsizetype pos ) |
| QBitArray & | operator&= (QBitArray && other ) |
| QBitArray & | operator&= (const QBitArray & other ) |
| QBitArray & | operator= (QBitArray && other ) |
| QBitArray & | operator= (const QBitArray & other ) |
| QBitRef | operator[] (qsizetype i ) |
| bool | operator[] (qsizetype i ) const |
| QBitArray & | operator^= (QBitArray && other ) |
| QBitArray & | operator^= (const QBitArray & other ) |
| QBitArray & | operator|= (QBitArray && other ) |
| QBitArray & | operator|= (const QBitArray & other ) |
| QBitArray | fromBits (const char * data , qsizetype size ) |
| bool | operator!= (const QBitArray & lhs , const QBitArray & rhs ) |
| QBitArray | operator& (QBitArray && a1 , QBitArray && a2 ) |
| QBitArray | operator& (QBitArray && a1 , const QBitArray & a2 ) |
| QBitArray | operator& (const QBitArray & a1 , QBitArray && a2 ) |
| QBitArray | operator& (const QBitArray & a1 , const QBitArray & a2 ) |
| QDataStream & | operator<< (QDataStream & out , const QBitArray & ba ) |
| bool | operator== (const QBitArray & lhs , const QBitArray & rhs ) |
| QDataStream & | operator>> (QDataStream & in , QBitArray & ba ) |
| QBitArray | operator^ (QBitArray && a1 , QBitArray && a2 ) |
| QBitArray | operator^ (QBitArray && a1 , const QBitArray & a2 ) |
| QBitArray | operator^ (const QBitArray & a1 , QBitArray && a2 ) |
| QBitArray | operator^ (const QBitArray & a1 , const QBitArray & a2 ) |
| QBitArray | operator| (QBitArray && a1 , QBitArray && a2 ) |
| QBitArray | operator| (QBitArray && a1 , const QBitArray & a2 ) |
| QBitArray | operator| (const QBitArray & a1 , QBitArray && a2 ) |
| QBitArray | operator| (const QBitArray & a1 , const QBitArray & a2 ) |
| QBitArray | operator~ (QBitArray a ) |
A QBitArray is an array that gives access to individual bits and provides operators ( AND , OR , XOR ,和 NOT ) that work on entire arrays of bits. It uses 隱式共享 (copy-on-write) to reduce memory usage and to avoid the needless copying of data.
The following code constructs a QBitArray containing 200 bits initialized to false (0):
QBitArray ba(200);
To initialize the bits to true, either pass
true
as second argument to the constructor, or call
fill
() later on.
QBitArray uses 0-based indexes, just like C++ arrays. To access the bit at a particular index position, you can use operator[](). On non-const bit arrays, operator[]() returns a reference to a bit that can be used on the left side of an assignment. For example:
QBitArray ba; ba.resize(3); ba[0] = true; ba[1] = false; ba[2] = true;
For technical reasons, it is more efficient to use testBit () 和 setBit () to access bits in the array than operator[](). For example:
QBitArray ba(3); ba.setBit(0, true); ba.setBit(1, false); ba.setBit(2, true);
QBitArray 支持
&
(
AND
),
|
(
OR
),
^
(
XOR
),
~
(
NOT
),及
&=
,
|=
,和
^=
. These operators work in the same way as the built-in C++ bitwise operators of the same name. For example:
QBitArray x(5); x.setBit(3, true); // x: [ 0, 0, 0, 1, 0 ] QBitArray y(5); y.setBit(4, true); // y: [ 0, 0, 0, 0, 1 ] x |= y; // x: [ 0, 0, 0, 1, 1 ]
For historical reasons, QBitArray distinguishes between a null bit array and an empty bit array. A null bit array is a bit array that is initialized using QBitArray's default constructor. An empty bit array is any bit array with size 0. A null bit array is always empty, but an empty bit array isn't necessarily null:
QBitArray().isNull(); // returns true QBitArray().isEmpty(); // returns true QBitArray(0).isNull(); // returns false QBitArray(0).isEmpty(); // returns true QBitArray(3).isNull(); // returns false QBitArray(3).isEmpty(); // returns false
所有函數除瞭 isNull () treat null bit arrays the same as empty bit arrays; for example, QBitArray() compares equal to QBitArray(0). We recommend that you always use isEmpty () and avoid isNull ().
另請參閱 QByteArray and QList .
Performs the AND operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] a &= b; // a: [ 1, 0, 0 ]
另請參閱 operator& (), operator|= (), operator^= (),和 operator~ ().
Performs the OR operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] a |= b; // a: [ 1, 1, 1 ]
另請參閱 operator| (), operator&= (), operator^= (),和 operator~ ().
Performs the XOR operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] a ^= b; // a: [ 0, 1, 1 ]
另請參閱 operator^ (), operator&= (), operator|= (),和 operator~ ().
[noexcept]
QBitArray::
QBitArray
()
構造空的位數組。
另請參閱 isEmpty ().
[explicit]
QBitArray::
QBitArray
(
qsizetype
size
,
bool
value
= false)
Constructs a bit array containing size bits. The bits are initialized with value , which defaults to false (0).
[noexcept]
QBitArray::
QBitArray
(const
QBitArray
&
other
)
構造副本為 other .
此操作花費 常量時間 ,因為 QBitArray 是 隱式共享 . This makes returning a QBitArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes 綫性時間 .
另請參閱 operator= ().
[noexcept]
QBitArray::
QBitArray
(
QBitArray
&&
other
)
Move-constructs a QBitArray instance, making it point at the same object that other 所指嚮的。
Returns the value of the bit at index position i .
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
另請參閱 operator[] ().
Returns a pointer to a dense bit array for this
QBitArray
. Bits are counted upwards from the least significant bit in each byte. The number of bits relevant in the last byte is given by
size() % 8
.
Clears the contents of the bit array and makes it empty.
Sets the bit at index position i 為 0。
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
另請參閱 setBit () 和 toggleBit ().
如同 size ().
若 on is true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned.
Sets every bit in the bit array to
value
, returning true if successful; otherwise returns
false
。若
size
is different from -1 (the default), the bit array is resized to
size
事先。
範例:
QBitArray ba(8); ba.fill(true); // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] ba.fill(false, 2); // ba: [ 0, 0 ]
另請參閱 resize ().
Sets bits at index positions begin up to (but not including) end to value .
begin must be a valid index position in the bit array (0 <= begin < size ()).
end must be either a valid index position or equal to size (), in which case the fill operation runs until the end of the array (0 <= end <= size ()).
範例:
QBitArray ba(4); ba.fill(true, 1, 2); // ba: [ 0, 1, 0, 0 ] ba.fill(true, 1, 3); // ba: [ 0, 1, 1, 0 ] ba.fill(true, 1, 4); // ba: [ 0, 1, 1, 1 ]
這是重載函數。
[static]
QBitArray
QBitArray::
fromBits
(const
char
*
data
,
qsizetype
size
)
創建 QBitArray with the dense bit array located at data ,采用 size bits. The byte array at data 必須至少 size / 8 (rounded up) bytes long.
若 size is not a multiple of 8, this function will include the lowest size % 8 bits from the last byte in data .
另請參閱 bits ().
返迴
true
若此位數組擁有大小 0;否則返迴 false。
另請參閱 size ().
返迴
true
若此位數組為 null;否則返迴
false
.
範例:
QBitArray().isNull(); // returns true QBitArray(0).isNull(); // returns false QBitArray(3).isNull(); // returns false
Qt makes a distinction between null bit arrays and empty bit arrays for historical reasons. For most applications, what matters is whether or not a bit array contains any data, and this can be determined using isEmpty ().
另請參閱 isEmpty ().
重置位數組大小到 size 位。
若 size is greater than the current size, the bit array is extended to make it size bits with the extra bits added to the end. The new bits are initialized to false (0).
若 size is less than the current size, bits are removed from the end.
另請參閱 size ().
Sets the bit at index position i 到 1。
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
另請參閱 clearBit () 和 toggleBit ().
Sets the bit at index position i to value .
這是重載函數。
Returns the number of bits stored in the bit array.
另請參閱 resize ().
[noexcept]
void
QBitArray::
swap
(
QBitArray
&
other
)
Swaps this bit array with other 。此操作很快且從不失敗。
返迴
true
if the bit at index position
i
為 1;否則返迴
false
.
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
[noexcept, since 6.0]
quint32
QBitArray::
toUInt32
(
QSysInfo::Endian
endianness
,
bool
*
ok
= nullptr) const
Returns the array of bit converted to an int. The conversion is based on
endianness
. Converts up to the first 32 bits of the array to
quint32
and returns it, obeying
endianness
。若
ok
is not a null pointer, and the array has more than 32 bits,
ok
is set to false and this function returns zero; otherwise, it's set to true.
該函數在 Qt 6.0 引入。
Inverts the value of the bit at index position i , returning the previous value of that bit as either true (if it was set) or false (if it was unset).
If the previous value was 0, the new value will be 1. If the previous value was 1, the new value will be 0.
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
Truncates the bit array at index position pos .
若 pos 超越數組末尾,什麼都不發生。
另請參閱 resize ().
[noexcept]
QBitArray
&QBitArray::
operator=
(
QBitArray
&&
other
)
移動 other to this bit array and returns a reference to this bit array.
[noexcept]
QBitArray
&QBitArray::
operator=
(const
QBitArray
&
other
)
賦值 other to this bit array and returns a reference to this bit array.
Returns the bit at index position i 作為可修改引用。
i must be a valid index position in the bit array (i.e., 0 <= i < size ()).
範例:
QBitArray a(3); a[0] = false; a[1] = true; a[2] = a[0] ^ a[1];
The return value is of type QBitRef, a helper class for QBitArray . When you get an object of type QBitRef, you can assign to it, and the assignment will apply to the bit in the QBitArray from which you got the reference.
函數 testBit (), setBit (),和 clearBit () 稍微更快。
另請參閱 at (), testBit (), setBit (),和 clearBit ().
這是重載函數。
Returns a bit array that is the AND of the bit arrays a1 and a2 .
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); QBitArray c; a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] c = a & b; // c: [ 1, 0, 0 ]
另請參閱 operator&= (), operator| (),和 operator^ ().
Returns a bit array that is the OR of the bit arrays a1 and a2 .
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); QBitArray c; a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] c = a | b; // c: [ 1, 1, 1 ]
另請參閱 QBitArray::operator|= (), operator& (),和 operator^ ().
Returns a bit array that is the XOR of the bit arrays a1 and a2 .
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
範例:
QBitArray a(3); QBitArray b(2); QBitArray c; a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b[0] = 1; b[1] = 1; // b: [ 1, 1 ] c = a ^ b; // c: [ 0, 1, 1 ]
另請參閱 operator^=() , operator&() ,和 operator|() .
[noexcept]
bool
operator!=
(const
QBitArray
&
lhs
, const
QBitArray
&
rhs
)
返迴
true
if
lhs
不等於
rhs
bit array; otherwise returns
false
.
另請參閱 operator== ().
寫入位數組 ba 到流 out .
另請參閱 QDataStream 運算符格式 .
[noexcept]
bool
operator==
(const
QBitArray
&
lhs
, const
QBitArray
&
rhs
)
返迴
true
if
lhs
等於
rhs
bit array; otherwise returns
false
.
另請參閱 operator!= ().
Reads a bit array into ba 從流 in .
另請參閱 QDataStream 運算符格式 .
Returns a bit array that contains the inverted bits of the bit array a .
範例:
QBitArray a(3); QBitArray b; a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] b = ~a; // b: [ 0, 1, 0 ]