QByteArray
QByteArray 是一个Qt框架中的类,它是一个可变长的字节数组,可以用于存储任意类型的数据,包括二进制数据和文本数据
// 创建数组
QByteArray byteArray; // 空的字节数组
QByteArray byteArray1("Hello world"); // 初始化为字符串
QByteArray byteArray2 = "Hello world"; // 等同于上面的初始化方式
QByteArray byteArray3(10, '\0'); // 创建一个长度为10的空字节数组//访问 QByteArray 中的数据:
char *data = byteArray.data(); // 获取字节数组的指针
int size = byteArray.size(); // 获取字节数组的长度
char byte = byteArray.at(0); // 获取字节数组中指定位置的字节//将 QByteArray 转换为 QString
QByteArray bytes("hello world");
// 方法1
QString str = QString::fromUtf8(byteArray); //将字节数组转换为UTF-8编码的字符串
// 方法2
QString string = bytes;
// 方法3
QString string;
string.prepend(bytes);//将 QString 转换为 QByteArray
QString str("hello");
// 方法1
QByteArray bytes = str.toUtf8(); // 将字符串转换为UTF-8编码的字节数组
//方法2
QByteArray bytes = str.toLatin1();
//1
QByteArray ba("Hello");//大小为 5 的字节数组
//2
QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;
ba[1] = 0xb8;
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;
//3,对于只读访问,另一种语法是使用 at()
for (int i = 0; i < ba.size(); ++i) {if (ba.at(i) >= 'a' && ba.at(i) <= 'f')cout << "Found character in range [a-f]" << endl;
}
//4,修改字节数据的基本函数:append()、prepend()、insert()、replace() 和 remove()
QByteArray x("and");
x.prepend("rock "); // x == "rock and"
x.append(" roll"); // x == "rock and roll"
x.replace(5, 3, "&"); // x == "rock & roll"// 在QByteArray第0个字节后插入一个字符
byteArray.insert(1, 'B');
// 在QByteArray第1个字节后插入一个字符串
byteArray.insert(2, "123");
// 删除4~6字节
byteArray.remove(4, 3);
// 获取QByteArray的大小
int size = byteArray.size();
// 获取QByteArray的指针(只读)
const char* data = byteArray.constData();QByteArray byteArray("Hello World");
QString str = QString::fromUtf8(byteArray);
// 获取QByteArray的data
const char* data = byteArray.data();
// 将QString转换为C字符串
QString str = "Hello World";
const char* c_str = str.toUtf8().data();
QString
1、Qt中的字符串类
(1)、采用Unicode编码(支持中文等)
(2)、使用隐式共享技术(集合了深拷贝浅拷贝)来节省内存和不必要的拷贝
(3)、跨平台使用,不需要考虑字符串的平台兼顾
2、Qstring VS string
(1)、Qstring 直接支持字符串和数字的互相转换
(2)、Qstring 直接支持字符串的比较大小
(3)、Qstring 直接支持不同字符编码间的相互转换
(4)、Qstring 直接支持std::string和std::wstring(宽)的相互转换
(5)、Qstring直接支持正则表达式的应用
(6)、Qstring在Qt库中几乎无所不在的,所有的Qt图形用户组件都依赖于Qstring
#include <QApplication>
#include <QDebug>
#include <QString>void Sample_1()
{QString s = "add";s.append(" "); //"add "s.append("Qt"); //"add Qt"s.prepend(" "); //" add Qt"s.prepend("C++");//"C++ and Qt"qDebug() << s;s.replace("add", "&"); //"C++ & Qt"qDebug() << s;
}void Sample_2()
{QString s = "";int index = 0;s.sprintf("%d. I'm %s, thank you!", 1, "SantaClaus"); //"1. I'm SantaClaus, thank you!"qDebug() << s;index = s.indexOf(",");//从索引0到index之间的字符子串s = s.mid(0, index); //"1. I'm SantaClaus"qDebug() << s;index = s.indexOf(".");s = s.mid(index + 1,s.length()); //" I'm SantaClaus";s = s.trimmed(); //"I'm SantaClaus";qDebug() << s;index = s.indexOf(" ");s = s.mid(index + 1, s.length()); //"SantaClaus"qDebug() << s;
}void Sample_3(QString* a, int len)
{for(int i=0;i<len;i++){for(int j=i+1; j<len; j++){if( a[j] < a[i]){QString tmp = a[i];a[i] = a[j];a[j] = tmp;}}}
}int main(int,char**)
{qDebug() << "Sample_1:"; Sample_1(); qDebug() << endl;qDebug() << "Sample_2:"; Sample_2(); qDebug() << endl;qDebug() << "Sample_3:"; QString company[5] ={QString("Oracle"),QString("Borland"),QString("Microsoft"),QString("IBM"),QString("Horizon Studio") };Sample_3(company, 5);for(int i=0; i<5; i++){qDebug() << company[i];} return 0;
}
QVector
#include <QtCore/QCoreApplication>
#include <QVector>
#include <iostream>
using namespace std;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QVector<int> vec;vec.push_back(9);vec.append(3);vec.push_back(7);vec.push_front(5);vec.push_front(2);vec.append(6);vec.insert(0, 11);vec.insert(0, 12);vec.insert(4, -1);//12 11 2 5 9 3 7 6for (int elem : vec){cout << elem << " , ";}cout << endl;//删除元素cout << "QVector删除数据操作" << endl;vec.pop_back();vec.pop_front();vec.remove(2, 1); //删除当前vec的第2个数据for (int elem : vec){cout << elem << " , ";}cout << endl;//当前vec的元素:11 2 -1 9 3 7cout << "迭代器用法" << endl;//QVector<int>::iterator it;//for (it = vec.begin(); it != vec.end(); it++)for(auto it= vec.begin(); it != vec.end(); it++){if (*it == -1){vec.erase(it); //删除掉-1}cout << *it << " ,";}cout << endl;vec.clear();cout << "清空后vec大小 = " << vec.size() << endl;return a.exec();
}
/*运行结果:12 , 11 , 2 , 5 , -1 , 9 , 3 , 7 , 6 ,QVector删除数据操作11 , 2 , -1 , 9 , 3 , 7 ,迭代器用法11 ,2 ,9 ,3 ,7 ,清空后vec大小 = 0*/
QHash
//1、创建;这里以键int,值QString示例
QHash<int,QString> qhash;
//2、插入//方式一qhash[1] = "1";qhash[2] = "2";qhash[3] = "3";//方式二qhash.insert(4, “4”);// qhash.insert(4,"10);先前的值将被删除
//3、取值//方式一 QString str1=qhash.value(1);//str1="1";//方式二QString str2=qhash[2];//str1="2";//4、检索某个值是否在里面if(qhash.contains(9)){return false;}if(qhash.contains(1)){return true;}
//5、查找某个字并获取QString str;if(qhash.contains(1)){str=qhash.value(1); }else{str=qhash.value(1,"wert");//如果哈希表中不存在指定键的元素,该函数使用第2个参数作为默认值}//6、遍历 QHash 中存储的所有键值对
QHash<int,QString>::const_iterator it = qhash.constBegin();
while (it != qhash.constEnd()) {cout << it.key() << ": " << it.value() << Qt::endl;++it;
}//7、删除,下面两种都可以qhash.erase(it);//这里的it是迭代器哦qhash.remove(key);