- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
std::vector::size()
返回一个无符号的 size_type
,通常与 size_t
相同,例如在 64 位平台上是 8 个字节。
相比之下,QVector::size()
返回一个 int
,即使在 64 位平台上通常也是 4 个字节,并且它是有符号的,这意味着它可以只走到2^32的一半。
这是为什么呢?这似乎很不合逻辑,而且在技术上也有限制,虽然您可能需要超过 2^32 个元素的可能性也不大,但使用有符号 int 会毫无理由地将该范围缩小一半。也许是为了避免对那些懒得将 i
声明为 uint
而不是决定让所有容器返回大小类型的 int
的人发出编译器警告这没有任何意义是更好的解决方案吗?原因不可能这么傻吧?
最佳答案
至少从 Qt 3 开始,这个问题已经讨论过好几次了,QtCore 维护者表示不久前不会发生任何变化,直到 Qt 7 才会发生。
当时进行讨论时,我认为迟早会有人在 Stack Overflow 上提出它……而且可能还会在其他几个论坛和 Q/A 上提出。让我们试着揭开这种情况的神秘面纱。
一般而言,您需要了解这里没有更好或更差,因为 QVector
不是 std::vector
的替代品。后者不执行任何写时复制 (COW),而且这是有代价的。基本上,它适用于不同的用例。它主要用于 Qt 应用程序和框架本身,最初用于早期的 QWidgets。
size_t
也有它自己的问题,毕竟我将在下面指出。
无需我向您解释维护者,我将直接引用蒂亚戈来传递 official stance 的信息。上:
For two reasons:
1) it's signed because we need negative values in several places in the API: indexOf() returns -1 to indicate a value not found; many of the "from" parameters can take negative values to indicate counting from the end. So even if we used 64-bit integers, we'd need the signed version of it. That's the POSIX ssize_t or the Qt qintptr.
This also avoids sign-change warnings when you implicitly convert unsigneds to signed:
-1 + size_t_variable => warning
size_t_variable - 1 => no warning
2) it's simply "int" to avoid conversion warnings or ugly code related to the use of integers larger than int.
size_t maxPathName = ::pathconf(nativePath.constData(), _PC_NAME_MAX);
if (maxPathName == size_t(-1))
if (len < 0 || len != qint64(size_t(len))) {
qint64 QIODevice::bytesToWrite() const
{
return qint64(0);
}
return readSoFar ? readSoFar : qint64(-1);
这是蒂亚戈的一封电子邮件,然后是 there is another你可以在哪里找到一些详细的答案:
Even today, software that has a core memory of more than 4 GB (or even 2 GB) is an exception, rather than the rule. Please be careful when looking at the memory sizes of some process tools, since they do not represent actual memory usage.
In any case, we're talking here about having one single container addressing more than 2 GB of memory. Because of the implicitly shared & copy-on-write nature of the Qt containers, that will probably be highly inefficient. You need to be very careful when writing such code to avoid triggering COW and thus doubling or worse your memory usage. Also, the Qt containers do not handle OOM situations, so if you're anywhere close to your memory limit, Qt containers are the wrong tool to use.
The largest process I have on my system is qtcreator and it's also the only one that crosses the 4 GB mark in VSZ (4791 MB). You could argue that it is an indication that 64-bit containers are required, but you'd be wrong:
Qt Creator does not have any container requiring 64-bit sizes, it simply needs 64-bit pointers
It is not using 4 GB of memory. That's just VSZ (mapped memory). The total RAM currently accessible to Creator is merely 348.7 MB.
And it is using more than 4 GB of virtual space because it is a 64-bit application. The cause-and-effect relationship is the opposite of what you'd expect. As a proof of this, I checked how much virtual space is consumed by padding: 800 MB. A 32-bit application would never do that, that's 19.5% of the addressable space on 4 GB.
(padding is virtual space allocated but not backed by anything; it's only there so that something else doesn't get mapped to those pages)
通过蒂亚戈的回答进一步探讨这个话题,请参阅:
Personally, I'm VERY happy that Qt collection sizes are signed. It seems nuts to me that an integer value potentially used in an expression using subtraction be unsigned (e.g. size_t).
An integer being unsigned doesn't guarantee that an expression involving that integer will never be negative. It only guarantees that the result will be an absolute disaster.
另一方面,C 和 C++ 标准定义了无符号的行为 上溢和下溢。
有符号整数不会上溢或下溢。我的意思是,他们这样做是因为类型 和 CPU 寄存器的位数有限,但标准说它们 不。这意味着编译器将始终优化假设您没有过度 - 或下溢。
例子:
for (int i = 1; i >= 1; ++i)
This is optimised to an infinite loop because signed integers do not overflow. If you change it to unsigned, then the compiler knows that it might overflow and come back to zero.
Some people didn't like that: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475
关于c++ - 为什么 QVector::size 返回 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619863/
如何解决以下问题,即转换 const QVector>至 QVector> ? 我尝试了一些步骤但没有帮助: QVector> points = const_cast>>(abc.points); a
我想知道 QVector> 对象中的元素总数,说 myobject .一种方法是设置一个 for 循环,如下所示: int siz; QVector temp; for(int i=0; i> 的大小
我正在编写一个程序来模拟缓存。我使用 QVector 作为结构。我有一个 QVector 的 QVectors of ints。但是,当我尝试编辑整数时,会导致运行时错误: *** glibc det
我已经声明过: //myclass.h QVector aux; QVector > tests; //myclass.cpp aux = (QVector(2)); 它工作正常,但现在我想将每个 t
我是 C++ 编程的新手,所以我可能只是遗漏了一些东西,但我已经尝试解决这个问题将近一个小时了。 我有一套 QLabels在我的 UI我想每秒更新两次。它更新的数据存储在 QVector 中尺寸为 1
我在 QT 中有以下变量: QVector > buff_d1; 我需要修改“内部” vector : buff_d1.at(i).removeFirst(); buff_d1.at(i).push_
我真的尝试了所有方法,但找不到解决方案,我尝试先初始化外部 QVector,然后初始化内部,但没有成功。 最佳答案 QVector *matrix (as class member) with new
谁能给我一个使用以下构造函数 int Qt 的例子? QVector::QVector(std::initializer_list args); 最佳答案 采用 std::initializer_li
为了完成这项工作,我真的尝试结合我所知道的一切。相信目前的结构不会出现死锁或其他线程问题。然而,有些部分丢失了,可用的文档对我没有帮助(我没有使用 C++ 文档的经验)。 int main() {
我试图将 100 000 个 QString 元素(每个 QString 中大约有 10 个字符)附加到 QVector。之后程序失败。 QVector 可以包含多少个元素是否有一些限制(当然除了物理
我有一个 QVector对于一个结构体,结构体包含许多属性,有些是 int,有些是指向类的指针。和每个 TheCore将绑定(bind)到 QTabWidget 中的选项卡. struct TheCo
如何制作动态数量的小部件的 QVector(或其他一些容器类),例如 QPushButton 或 QComboBox Qt 4? 我在窗口类的构造函数中使用了以下内容: QVector foo; //
这个问题在这里已经有了答案: How does the Comma Operator work (9 个回答) 关闭 6 年前。 我正在尝试分配 QVector 的一部分Qt 中的 double 组
我遇到了问题。 出于某种原因,尽管我在 QString 中有一个值,但对 QVector 执行 append() 不起作用,我最终仍然只有一个空 vector 。 QList listServ
假设我有一个 QVector 存储这些元素:{3, 4, 1, 5, 6}。所以如果我从不同的线程读取每个元素,这会导致错误共享吗? (“读取”的意思是这样的:int i = vector[0];//
我正在尝试创建一个多维数组(C++ 数组类)的 QVector,但我遇到了麻烦 我有一个“节点”类,我想将多维数组的 QVector 作为节点类构造的参数传递,但这不起作用,我遇到了编译错误! Cla
所以我正在尝试创建一个简单的程序,允许我使用随机函数来操纵 vector ,但我觉得我忘记了一些相当重要和基本的东西请随时查看我发布的代码,让我知道解决方案是什么。我收到的错误是 myvec 是“索引
Qt 容器在被多线程使用时作为只读是安全的。但是写访问呢?我可以调整 QVector 的大小并在不同的线程中使用 operator[] 进行写入吗? QVector 的大小将保持不变,每个线程将写入不
我正在尝试将 QVector 与名为 RoutineItem 的自定义对象一起使用。 但是报错: C:\Qt\5.2.1\mingw48_32\include\QtCore\qvector.h:265
我可以看到类被视为调用默认构造函数所需的复杂对象: void QVector::defaultConstruct(T *from, T *to) { if (QTypeInfo::isComp
我是一名优秀的程序员,十分优秀!