简体   繁体   English

如何初始化 QVector

[英]How to initialize QVector

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."我原以为 QVector 已经分配了 100 个索引,但是当我尝试读取myVector[0]我收到一个断言错误,提示“test.exe 中 0x0143bf77 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004”。 and the program stops at this line of Qt:程序停在 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)但我想了解什么是错误的以及如何使它正确。

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 main.cpp中

#include <QVector>
#include <QDebug>

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

main.pro 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 . 正如我在评论中提到的,您可以使用保留方法

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. 如果你事先知道向量的大小,你可以调用这个函数,如果你经常调用resize(),你可能会获得更好的性能。 If size is an underestimate, the worst that will happen is that the QVector will be a bit slower. 如果大小被低估,那么最糟糕的情况是QVector会慢一点。

The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. 此功能的唯一目的是提供一种微调QVector内存使用的方法。 In general, you will rarely ever need to call this function. 通常,您很少需要调用此函数。 If you want to change the size of the vector, call resize(). 如果要更改向量的大小,请调用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. 它至少比您需要的代码多。

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. 如果你事先知道向量的大小,你可以调用这个函数,如果你经常调用resize(),你可能会获得更好的性能。 If size is an underestimate, the worst that will happen is that the QVector will be a bit slower. 如果大小被低估,那么最糟糕的情况是QVector会慢一点。 The sole purpose of this function is to provide a means of fine tuning QVector's memory usage. 此功能的唯一目的是提供一种微调QVector内存使用的方法。 In general, you will rarely ever need to call this function. 通常,您很少需要调用此函数。 If you want to change the size of the vector, call resize(). 如果要更改向量的大小,请调用resize()。

But this will only reserve the memory, if you want to browse it, use fill(<double>) 但这只会保留内存,如果要浏览它,请使用fill(<double>)

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

void QVector::resize(int size) Sets the size of the vector to size. void QVector::resize(int size) 将向量的大小设置为 size。 If size is greater than the current size, elements are added to the end;如果 size 大于当前大小,则将元素添加到末尾; the new elements are initialized with a default-constructed value.新元素使用默认构造值进行初始化。 If size is less than the current size, elements are removed from the end.如果 size 小于当前大小,则从末尾删除元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM