C++ 如何初始化 QVector

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20933509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:24:44  来源:igfitidea点击:

How to initialize QVector

c++qtqtcoreqvector

提问by mFeinstein

I am new to c++ and Qt and I am trying to initialize a QVector, which is a class member in a class initialization list like:

我是 C++ 和 Qt 的新手,我正在尝试初始化一个 QVector,它是类初始化列表中的类成员,例如:

MyClass::MyClass(QWidget *parent) : QMainWindow(parent) , myVector(QVector<double>(100))

I was expecting the QVector to have already 100 indexes alocated, but when I try to read myVector[0]I get an assertion error saying "Unhandled exception at 0x0143bf77 in test.exe: 0xC0000005: Access violation reading location 0x00000004." and the program stops at this line of Qt:

我原以为 QVector 已经分配了 100 个索引,但是当我尝试读取时,myVector[0]我收到一个断言错误,提示“test.exe 中 0x0143bf77 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004”。程序停在 Qt 的这一行:

inline T &QVector<T>::operator[](int i)
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
  return data()[i]; }

Which I believe shows that I am trying to access members that arent allocated yet, so I guess I am not using the initialization list properly. I could make it a pointer and make a new QVector(100)in the constructor but I want to learn what's wrong and how may I make it correct.

我相信这表明我正在尝试访问尚未分配的成员,所以我想我没有正确使用初始化列表。我可以让它成为一个指针,并new QVector(100)在构造函数中创建一个,但我想了解什么是错误的以及如何使它正确。

回答by lpapp

You are probably doing something wrong unshown because the following code works fine for me, and it should by design. Note that for the first element, you could use the convenience first method.

您可能在未显示的情况下做错了什么,因为以下代码对我来说工作正常,并且应该按照设计。请注意,对于第一个元素,您可以使用方便的第一个方法

main.cpp

主程序

#include <QVector>
#include <QDebug>

int main()
{
    QVector<double> myVector(QVector<double>(100));
    qDebug() << "TEST FIRST:" << myVector.first();
    return 0;
}

main.pro

主程序

TEMPLATE = app
TARGET = main
SOURCES += main.cpp

Output

输出

TEST FIRST: 0

As I noted in the comment, you could use the reserve method.

正如我在评论中指出的,您可以使用Reserve 方法

void QVector::reserve(int size)

Attempts to allocate memory for at least size elements. If you know in advance how large the vector will be, you can call this function, and if you call resize() often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QVector will be a bit slower.

The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the vector, call resize().

void QVector::reserve(int size)

尝试为至少 size 个元素分配内存。如果你事先知道向量有多大,你可以调用这个函数,如果你经常调用 resize() 你可能会获得更好的性能。如果大小被低估,最坏的情况是 QVector 会慢一点。

此函数的唯一目的是提供一种微调 QVector 内存使用的方法。通常,您很少需要调用此函数。如果要更改向量的大小,请调用 resize()。

So, you would be writing something like this:

所以,你会写这样的东西:

MyClass::MyClass(QWidget *parent)
    : QMainWindow(parent)
{
    myVector.reserve(100);
}

However, as I also noted later in the comment, the simple constructor should also work like:

但是,正如我稍后在评论中所指出的,简单的构造函数也应该像这样工作:

MyClass::MyClass(QWidget *parent)
    : QMainWindow(parent)
    , myVector(100)
{
}

What you are doing is invoking the copy constructor (although for an implicitly shared class), so it may be negligibly slower. It is at least more code than you need.

您正在做的是调用复制构造函数(尽管对于隐式共享类),因此它的速度可能会慢到可以忽略不计。它至少比您需要的代码多。

回答by Thomas Ayoub

Try something like this :

尝试这样的事情:

QVector<double> * vect = new QVector<double>;
vect->reserve(100);

void QVector::reserve(int size)

void QVector::reserve(int size)

Attempts to allocate memory for at least size elements. If you know in advance how large the vector will be, you can call this function, and if you call resize() often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QVector will be a bit slower. The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the vector, call resize().

尝试为至少 size 个元素分配内存。如果你事先知道向量有多大,你可以调用这个函数,如果你经常调用 resize() 你可能会获得更好的性能。如果大小被低估,最坏的情况是 QVector 会慢一点。此函数的唯一目的是提供一种微调 QVector 内存使用的方法。通常,您很少需要调用此函数。如果要更改向量的大小,请调用 resize()。

But this will only reserve the memory, if you want to browse it, use fill(<double>)

但这只会保留内存,如果你想浏览它,使用 fill(<double>)