Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Modifying Algorithms | Algorithms
C++ STL Containers and Algorithms

bookModifying 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

main.cpp

copy
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

main.cpp

copy
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 << " "; }
Note
Note

Always pair std::remove with erase to avoid leftover invalid data at the end of the container.

question mark

Which statement best explains why you must use the erase-remove idiom with std::vector when removing elements using std::remove?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 6.67

bookModifying Algorithms

Swipe um das Menü anzuzeigen

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

main.cpp

copy
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

main.cpp

copy
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 << " "; }
Note
Note

Always pair std::remove with erase to avoid leftover invalid data at the end of the container.

question mark

Which statement best explains why you must use the erase-remove idiom with std::vector when removing elements using std::remove?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
some-alt