27 lines
732 B
Plaintext
27 lines
732 B
Plaintext
== Why is this an issue?
|
|
|
|
Container member functions that modify the containers often take an iterator as input to specify a precise location work on. If this iterator comes from a different container than the one calling the function, the result will be undefined.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,cpp]
|
|
----
|
|
void test(std::vector &v1, std::vector &v2) {
|
|
v1.insert(v2.begin(), v2.begin(), v2.end()); // Noncompliant, the first argument is the insertion location and must be in v1
|
|
v1.erase(v2.begin()); // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,cpp]
|
|
----
|
|
void test(std::vector &v1, std::vector &v2) {
|
|
v1.insert(v1.begin(), v2.begin(), v2.end()); // Compliant
|
|
v1.erase(v1.begin()); // Compliant
|
|
}
|
|
----
|
|
|