Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about a QList<QStringList> in Qt

Tags:

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?

like image 894
yan bellavance Avatar asked Oct 15 '09 00:10

yan bellavance


People also ask

How do you iterate through a Qstringlist?

Iterating Over the Strings To iterate over a list, you can either use index positions or QList's Java-style and STL-style iterator types: Indexing: for (int i = 0; i < fonts. size(); ++i) cout << fonts.at(i).

How do you reverse a QList?

You may write them, or you can just write copying function on your own -- reversing a list is a nice excersize. Or you can note that QList is actually an array, what makes writing such a function trivial. Or you can convert the list to std::list, and use rbegin and rend. Choose whatever you like.

What is QList in Qt?

QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.


1 Answers

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.

like image 114
shoosh Avatar answered Oct 13 '22 08:10

shoosh