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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 6.67
Modifying Algorithms
Svep för att visa menyn
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.
Tack för dina kommentarer!