建站能赚钱吗,网站开发招标参数,网站优化营销,宁乡县住房和城乡建设局网站QByteArray QByteArray 是一个Qt框架中的类#xff0c;它是一个可变长的字节数组#xff0c;可以用于存储任意类型的数据#xff0c;包括二进制数据和文本数据 // 创建数组 QByteArray byteArray; // 空的字节数组 QByteArray byteArray1(Hello world); // 初始…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 QStringvoid Sample_1()
{QString s add;s.append( ); //add s.append(Qt); //add Qts.prepend( ); // add Qts.prepend(C);//C and QtqDebug() s;s.replace(add, ); //C QtqDebug() s;
}void Sample_2()
{QString s ;int index 0;s.sprintf(%d. Im %s, thank you!, 1, SantaClaus); //1. Im SantaClaus, thank you!qDebug() s;index s.indexOf(,);//从索引0到index之间的字符子串s s.mid(0, index); //1. Im SantaClausqDebug() s;index s.indexOf(.);s s.mid(index 1,s.length()); // Im SantaClaus;s s.trimmed(); //Im SantaClaus;qDebug() s;index s.indexOf( );s s.mid(index 1, s.length()); //SantaClausqDebug() s;
}void Sample_3(QString* a, int len)
{for(int i0;ilen;i){for(int ji1; jlen; 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 i0; i5; 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);QVectorint 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;//QVectorint::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示例
QHashint,QString qhash;
//2、插入//方式一qhash[1] 1;qhash[2] 2;qhash[3] 3;//方式二qhash.insert(4, “4”);// qhash.insert(4,10);先前的值将被删除
//3、取值//方式一 QString str1qhash.value(1);//str11;//方式二QString str2qhash[2];//str12;//4、检索某个值是否在里面if(qhash.contains(9)){return false;}if(qhash.contains(1)){return true;}
//5、查找某个字并获取QString str;if(qhash.contains(1)){strqhash.value(1); }else{strqhash.value(1,wert);//如果哈希表中不存在指定键的元素该函数使用第2个参数作为默认值}//6、遍历 QHash 中存储的所有键值对
QHashint,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);