QT学习笔记

1、QString与char*的转换

QString --> char*

QString strQ;
std::string strStd = strQ.toStdString();
const char* strCon = strStd.c_str();

char* --> QString

char* str = "Hello world";
QString strQ = QString(QLatin1String(str));

QString与string的相互转换

1、QString--> int

QString qstr = QString::number(123);

int i = atoi(qstr.toStdString().c_str());

也可以这样:int i = atoi(qstr.ascii());

2、QString-->string,即std::string

string s = qstr.toStdString();

QString qstr2 = QString::fromStdString(s);


QList和QVector等容器的区别。

1、大多数情况下可以用QList。像prepend()和insert()这种操作,通常QList比QVector快的多。这是因为QList是基于index标签存储它的元素项在内存中,比那种依赖iterator迭代的更快捷。而且你的代码也更少。

2、如果你需要一个真正的连接着的list,且需要保证一个固定插入耗时。那就用迭代器,而不是标签。使用QLinkedList();

3、如果你需要开辟连续的内存空间存储,或者你的元素远比一个指针大,这时你需要避免个别插入操作,出现堆栈溢出,这时候用QVector

4、如果你需要一个低层的可变数量大小的数组,用QVarLengthArray就够了。他可以预先在栈中分配已知长度大小的数组,如果超过这个长度,会在堆中继续存储。默认大小256

QVarLengthArray<int, 1024> array(n + 1);

 

 

  • For most purposes, QList is the right class to use. Operations like prepend() and insert() are usually faster than with QVector because of the way QList stores its items in memory (see Algorithmic Complexity for details), and its index-based API is more convenient than QLinkedList's iterator-based API. It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
  • If you want the items to occupy adjacent memory positions, or if your items are larger than a pointer and you want to avoid the overhead of allocating them on the heap individually at insertion time, then use QVector.
  • If you want a low-level variable-size array, QVarLengthArray may be sufficient.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值