简体   繁体   English

如何拥有 QMap 的 QVector

[英]how to have a QVector of QMap

I wanna have a QVector of QMap.我想要一个 QMap 的 QVector。 I used this syntax:我使用了这个语法:

QVector<QMap<QString, QString>> x;
x[0].insert("name", "jim");
x[0].insert("lname", "helpert");
x[1].insert("name", "dwight");
x[1].insert("lname", "schrute");

but this is not working:但这不起作用:

在此处输入图像描述

I'd appreciate it if someone guide me to the correct format.如果有人指导我使用正确的格式,我将不胜感激。

You are getting "Index Out Of Range" because you are accessing an empty QVector.您正在获取“索引超出范围”,因为您正在访问一个空的 QVector。 You need to first insert QMap elements to QVector.您需要先将 QMap 元素插入到 QVector。 Then you can access x[0] -> for first QMap at 0th index, x[1] -> for second QMap at 1st index....... Make QMap object.然后您可以访问 x[0] -> 用于第 0 个索引处的第一个 QMap,x[1] -> 用于第 1 个索引处的第二个 QMap....... 制作 QMap object。 Insert Elments to it.向其中插入元素。 Make QVector object.制作 QVector object。 Insert that QMap object to this QVector.将 QMap object 插入到此 QVector。 Read the documents and use appropriate functions for it https://doc.qt.io/archives/qt-4.8/阅读文档并为其使用适当的功能https://doc.qt.io/archives/qt-4.8/

The "Index Out of Range" error comes up because you are trying to access an element of the vector which doesn't exist.出现“索引超出范围”错误是因为您试图访问不存在的向量元素。 Instead of accessing a particular index/element of the array it would be better to create a QMap outside of the QVector first and then x.push_back(map) so the map will be happily placed at the back of the QVector.与其访问数组的特定索引/元素,不如先在 QVector 之外创建一个 QMap,然后再创建一个x.push_back(map) ,这样 map 将愉快地放置在 QVector 的后面。

A similar thing applies to normal C++ with std::vector as you need to either push_back or emplace_back data onto the vector类似的事情适用于带有std::vector的普通 C++,因为您需要将push_backemplace_back数据推送到向量上

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

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