Home > Backend Development > C++ > Is Erasing Elements from a `std::set` While Iterating Safe and How to Do It Correctly?

Is Erasing Elements from a `std::set` While Iterating Safe and How to Do It Correctly?

DDD
Release: 2024-12-11 09:47:18
Original
515 people have browsed it

Is Erasing Elements from a `std::set` While Iterating Safe and How to Do It Correctly?

Deleting Elements from std::set While Iterating: Is It Safe?

Iterating through a set and removing elements that meet certain criteria is a common task in C programming. However, the behavior of erasing elements while iterating may raise concerns about invalidating iterators.

In the provided code snippet, you iterate through a set of integers and erase even numbers. While this code seems to work, it relies on implementation-specific behavior. The C standard does not explicitly specify what happens when an element is erased from a set while you're iterating through it.

Standard Conforming Approach

To ensure portability and reliability, consider using a standard conforming approach:

for (auto it = numbers.begin(); it != numbers.end(); ) {
    if (*it % 2 == 0) {
        numbers.erase(it++);
    }
    else {
        ++it;
    }
}
Copy after login

This approach uses the postfix increment operator it , which passes the old iterator to erase but advances it to point to the next element before returning. As a result, the loop iterates correctly, and the iterator remains valid after each erase operation.

C 11 Solution

C 11 introduced an updated erase method for set that simplifies the process of erasing elements while iterating:

for (auto it = numbers.begin(); it != numbers.end(); ) {
    if (*it % 2 == 0) {
        it = numbers.erase(it);
    }
    else {
        ++it;
    }
}
Copy after login

This approach uses the erase method, which returns an iterator to the element following the erased element (or set::end if the last element is erased). This simplifies the loop by eliminating the need to use postfix increment and ensuring that it remains valid after each erase operation.

Conclusion

While the initial code snippet may work in some implementations, relying on implementation-specific behavior is generally not recommended. By using a standard conforming approach or the C 11 erase method, you can ensure that your code works correctly and portably across different compilers and platforms.

The above is the detailed content of Is Erasing Elements from a `std::set` While Iterating Safe and How to Do It Correctly?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template