Modifying Algorithms
Modifying algorithms in the C++ Standard Library are essential tools for changing the contents of containers efficiently and expressively. The most commonly used modifying algorithms include std::copy, std::replace, and std::remove.
These algorithms operate on iterator ranges, making them flexible and container-agnostic.
main.cpp
1234567891011121314151617#include <iostream> #include <list> #include <algorithm> int main() { std::list<int> numbers = {1, 2, 3, 2, 4, 2, 5}; // Replace all occurrences of 2 with 99 std::replace(numbers.begin(), numbers.end(), 2, 99); // Remove all elements greater than 90 using remove_if numbers.remove_if([](int n){ return n > 90; }); for (int n : numbers) std::cout << n << " "; }
When using containers like std::vector or std::deque, algorithms such as std::remove don’t actually delete elements. Instead, they shift the remaining ones to the front and return an iterator to the new logical end, leaving the container size unchanged. To permanently remove elements, call the container’s erase function with that iterator — a common pattern known as the erase-remove idiom.
main.cpp
1234567891011121314#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 2, 4, 2, 5}; // Remove all occurrences of 2 auto new_end = std::remove(numbers.begin(), numbers.end(), 2); numbers.erase(new_end, numbers.end()); for (int n : numbers) std::cout << n << " "; }
Always pair std::remove with erase to avoid leftover invalid data at the end of the container.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain how the erase-remove idiom works with an example?
What are some common mistakes when using these modifying algorithms?
Are there other important modifying algorithms in the C++ Standard Library?
Awesome!
Completion rate improved to 6.67
Modifying Algorithms
Scorri per mostrare il menu
Modifying algorithms in the C++ Standard Library are essential tools for changing the contents of containers efficiently and expressively. The most commonly used modifying algorithms include std::copy, std::replace, and std::remove.
These algorithms operate on iterator ranges, making them flexible and container-agnostic.
main.cpp
1234567891011121314151617#include <iostream> #include <list> #include <algorithm> int main() { std::list<int> numbers = {1, 2, 3, 2, 4, 2, 5}; // Replace all occurrences of 2 with 99 std::replace(numbers.begin(), numbers.end(), 2, 99); // Remove all elements greater than 90 using remove_if numbers.remove_if([](int n){ return n > 90; }); for (int n : numbers) std::cout << n << " "; }
When using containers like std::vector or std::deque, algorithms such as std::remove don’t actually delete elements. Instead, they shift the remaining ones to the front and return an iterator to the new logical end, leaving the container size unchanged. To permanently remove elements, call the container’s erase function with that iterator — a common pattern known as the erase-remove idiom.
main.cpp
1234567891011121314#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 2, 4, 2, 5}; // Remove all occurrences of 2 auto new_end = std::remove(numbers.begin(), numbers.end(), 2); numbers.erase(new_end, numbers.end()); for (int n : numbers) std::cout << n << " "; }
Always pair std::remove with erase to avoid leftover invalid data at the end of the container.
Grazie per i tuoi commenti!