2014年03月03日 星期一 09:14
Qt中的QBitArray类似于C++ STL中的bitset,是专门对存储bit数据而优化设计的容器。典型的应用:将IP地址范围映射为QBitArray,然后就可以以O(1)的效率判断访问IP地址是否匹配某个范围。
The QBitArray class provides an array of bits.
A QBitArray is an array that gives access to individual bits and provides operators (AND, OR, XOR, and NOT) that work on entire arrays of bits. It uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data.
示例代码:
#include <QCoreApplication> #include <QtCore> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); auto bitary=QBitArray(10); bitary.fill(true); bitary.resize(15); qDebug() << bitary; for(int i=0;i<bitary.size();i++){ qDebug() << bitary.at(i); if(i % 2 ==0){ bitary.toggleBit(i); } } auto zeroary=QBitArray(bitary); zeroary.fill(false); qDebug() << bitary; qDebug() << zeroary; auto orary=bitary^zeroary; qDebug() << orary; return a.exec(); }
参考资料:
Zeuux © 2024
京ICP备05028076号