Home > Backend Development > C++ > body text

How to Safely Remove Elements from a std::vector During Iteration?

Patricia Arquette
Release: 2024-10-30 07:35:02
Original
349 people have browsed it

How to Safely Remove Elements from a std::vector During Iteration?

Removing Elements from a std::Vector During Iteration

When you have a vector of elements and need to remove items while iterating through it, the erase() method can cause difficulties. Erasing an element invalidates all iterators pointing to subsequent elements.

Iterators and erase()

Consider the following code:

<code class="cpp">std::vector<std::string> m_vPaths;
...
for (auto iter = m_vPaths.begin(); iter != m_vPaths.end(); iter++) {
  std::string strPath = *iter;
  if (::DeleteFile(strPath.c_str())) {
    m_vPaths.erase(iter);
    // Iterators become invalid after erase
  }
}</code>
Copy after login

After the first successful file deletion, the iterator pointing to the next element becomes invalid. This complicates the continuation of the iteration.

Using Two Vectors

One solution is to use two vectors: one to iterate through and another to store the elements to be deleted. While this works, it introduces additional memory overhead and complexity.

Alternative Data Structures

There are data structures that are more suitable for removing elements during iteration, such as:

  • std::set> (ordered and unique, naturally sorted in descending order)
  • std::unordered_set (unordered and unique)

These structures maintain a sorted or unordered collection of values, and removal does not invalidate other elements.

Using std::remove_if

Alternatively, you can use the std::remove_if() algorithm to remove specific elements from the vector before deleting them. This ensures that iterators remain valid:

<code class="cpp">auto iter_new_end = std::remove_if(m_vPaths.begin(), m_vPaths.end(),
  [](const std::string& strPath) {
    return ::DeleteFile(strPath.c_str());
  });
m_vPaths.erase(iter_new_end, m_vPaths.end());</code>
Copy after login

Conclusion

Depending on your specific requirements, you can choose the best approach from:

  • Using iterators with caution
  • Maintaining two vectors
  • Utilizing alternative data structures
  • Employing std::remove_if()

The above is the detailed content of How to Safely Remove Elements from a std::vector During Iteration?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!