diff options
Diffstat (limited to 'src/corelib/doc/snippets/code/doc_src_containers.cpp')
-rw-r--r-- | src/corelib/doc/snippets/code/doc_src_containers.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp index 350b2a91f2..6e59a8a548 100644 --- a/src/corelib/doc/snippets/code/doc_src_containers.cpp +++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp @@ -273,3 +273,33 @@ QString onlyLetters(const QString &in) return out; } //! [23] + +//! [24] +QVector<int> a, b; +a.resize(100000); // make a big vector filled with 0. + +QVector<int>::iterator i = a.begin(); +// WRONG way of using the iterator i: +b = a; +/* + Now we should be careful with iterator i since it will point to shared data + If we do *i = 4 then we would change the shared instance (both vectors) + The behavior differs from STL containers. Avoid doing such things in Qt. +*/ + +a[0] = 5; +/* + Container a is now detached from the shared data, + and even though i was an iterator from the container a, it now works as an iterator in b. + Here the situation is that (*i) == 0. +*/ + +b.clear(); // Now the iterator i is completely invalid. + +int j = *i; // Undefined behavior! +/* + The data from b (which i pointed to) is gone. + This would be well-defined with STL containers (and (*i) == 5), + but with QVector this is likely to crash. +*/ +//! [24] |