Removing elements from a container or asking if an associative container has a specific key is too complicated. I should say it was because with C++20, the story changes.
Let me start simply. You want to erase an element from a container.
The erase-remove Idiom
Okay. Removing an element from a container is quite easy. In the case of a
std::vector you can use the function
removeElemtens.cpp removes all elements the
std::vector that is smaller than zero. Easy, or? Now, you fall into the well-known trap to each professional C++ programmer.
std::remove_if inline (1) does not remove anything. The
std::vector still has the same number of arguments. Both algorithms return the new logical end of the modified container.
To modify a container, you must apply the new logical end to the container.
Line (1) returns the new logical end
newEnd of the container
myVec. This new logical end is applied in line (2) to remove all elements from
myVec starting at
newEnd. When you apply the functions remove and erase in one expression, such as in line (3), you exactly see why this construct is called erase-remove-idiom.
Thanks to the new functions
erase_if in C++20, erasing elements from containers is way more convenient.
erase_if in C++20
erase_if, you can directly operate on the container. In contrast, the previously presented erase-remove idiom is quite lengthy (line 3 in
erase requires two iterators which I provided by the algorithm
Let’s see what the new functions
erase_if mean in practice. The following program erases elements for a few containers.
Line (1) erases all character
e from the given string
str. Line (2) applies the lambda expression to the same string and erases all upper-case letters.
In the remaining program, elements of the sequence containers
std::vector (line 3),
std::deque (line 4), and
std::list (line 5) are erased. On each container, the function template
doAll (line 6) is applied.
doAll erases the element 5 and all elements greater than 3. The function template
erase (line 7) uses the new function
erase , and the function template
erasePredicate (line 8) uses the new function
Thanks to the Microsoft Compiler, here is the output of the program.
The new functions
erase_if can be applied to all containers of the Standard Template Library. This does not hold for the following convenience function
Checking the Existence of an Element in an Associative Container
Thanks to the functions
contains, you can quickly check if an element exists in an associative container.
Stopp, you may say, we can already do this with find or count.
No, both functions are not beginners friendly and have their downsides.
The functions produce the expected result.
Here are the issues with both calls. The
find call inline (1) is too lengthy. The same argumentation holds for the
count call in line (2). The
count call also has a performance issue. When you want to know if an element is in a container, stop when you found it and not count until the end. In the concrete case,
myMultiSet.count(2) returned 2.
On the contrary, the contains member function in C++20 is quite convenient.
There is not much to add to this example. The function template
true if the associative container contains the key 5. In my example, I only used the associative containers
std::unordered_set which can not have a key more than once.
The convenience functions go on in my next post. With C++20, you can calculate the midpoint of two values, check if a
std::string start or ends with a substring, and create callables with
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,