关于 QList的问题在 Qt 中

发布于 2024-08-08 06:37:42 字数 350 浏览 4 评论 0原文

我正在使用 Qt 4.5,并且正在使用 QList 它是字符串列表的列表。

现在我想替换一个 stringList 中的一个字符串,但输入起来似乎很不寻常。我找到了以下方法,想知道是否可以:

QList <QStringList> pDataList;

pDataList[listIndex].replace(QStringIndex, newString);

现在,我不担心语法,但我想知道 pDataList 的指针在内存中是否相邻,所以使用 [] 是可以的。还有其他方法吗?

I'm using Qt 4.5 and im working with a QList<QStringList> which is a list of string list.

Now I want to replace one string inside one stringList but with it seems unusual to type. I have found the following way of doing it and was wondering if it is ok:

QList <QStringList> pDataList;

pDataList[listIndex].replace(QStringIndex, newString);

Now, I'm not worried about the syntax, but I want to know if pDataList's pointers is adjascent in memory so that it is okay to use []. Is there another way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-08-15 06:37:42

pDataList的元素在内存中相邻的问题与是否可以使用operator[]的问题无关。

一般来说,QList<> 不保证其元素在内存中相邻,但它确实会重载 operator[] 来为您提供您正在查找的元素。

执行此类操作时,您唯一需要担心的是确保 (pDataList.size() < listIndex)。否则,您将索引不在列表中的元素,从而在调试中触发异常或在发布中触发未定义的行为。

The issue of pDataList's elements being adjacent in memory is not related to the question of whether it is ok to use operator[].

In general, QList<> does not guarantee that its elements are adjacent in memory, but it does overload operator[] to give you the element you're looking for nonetheless.

The only thing you need to worry about when doing something like this is to make sure that (pDataList.size() < listIndex). otherwise, you'll be indexing elements not in the list, triggering an exception in debug or undefined behavior in release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文