Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

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

bookModifying 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

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3
some-alt