{"id":5995,"date":"2020-09-24T20:36:08","date_gmt":"2020-09-24T20:36:08","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/more-convenience-functions-for-containers-with-c-20\/"},"modified":"2020-09-24T20:36:08","modified_gmt":"2020-09-24T20:36:08","slug":"more-convenience-functions-for-containers-with-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/more-convenience-functions-for-containers-with-c-20\/","title":{"rendered":"More Convenience Functions for Containers with C++20"},"content":{"rendered":"<p>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.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"700\" height=\"275\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Let me start simply. You want to erase an element from a container.&nbsp;<\/p>\n<h2>The erase-remove Idiom<\/h2>\n<p>Okay. Removing an element from a container is quite easy. In the case of a<code> std::vecto<\/code>r you can use the function<code>&nbsp;std::remove.&nbsp;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ removeElements.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>vector myVec{<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">5<\/span> };\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> ele<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> ele <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>remove_if(myVec.begin(), myVec.end(), [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> ele){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> ele <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">0<\/span>; }); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> ele<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> ele <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program<code> removeElemtens.cpp<\/code> removes all elements the <code>std::vector<\/code> that is smaller than zero. Easy, or? Now, you fall into the well-known trap to each professional&nbsp; C++ programmer.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5990\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemove.png\" alt=\"eraseRemove\" width=\"450\" height=\"267\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemove.png 529w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemove-300x178.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p><code>std::remove <\/code>or<code> std::remove_if<\/code>&nbsp; inline (1) does not remove anything. The <code>std::vector<\/code>&nbsp;still has the same number of arguments. Both algorithms return the new logical end of the modified container.&nbsp;<\/p>\n<p>To modify a container, you must apply the new logical end to the container.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ eraseRemoveElements.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>vector myVec{<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">5<\/span> };\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> ele<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> ele <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> newEnd <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>remove_if(myVec.begin(), myVec.end(),        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span><br \/>                                 [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> ele){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> ele <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">0<\/span>; });\r\n    myVec.erase(newEnd, myVec.end());                               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ myVec.erase(std::remove_if(myVec.begin(), myVec.end(),       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span><br \/>                   [](int ele){ return ele &lt; 0; }), myVec.end());<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> ele<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> ele <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line (1) returns the new logical end <code>newEnd<\/code> of the container <code>myVec<\/code>. This new logical end is applied in line (2) to remove all elements from <code>myVec<\/code> starting at <code>newEnd<\/code>. 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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5991\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemoveElements.png\" alt=\"eraseRemoveElements\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"529\" height=\"314\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemoveElements.png 529w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseRemoveElements-300x178.png 300w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/p>\n<p>Thanks to the new functions <code>erase<\/code> and <code>erase_if<\/code> in C++20, erasing elements from containers is way more convenient.<\/p>\n<\/p>\n<h2><code>erase<\/code> and <code>erase_if<\/code> in C++20<\/h2>\n<p>With <code>erase<\/code> and <code>erase_if<\/code>, you can directly operate on the container. In contrast, the previously presented erase-remove idiom is quite lengthy (line 3 in<code> eraseRemoveElements.cpp<\/code>): <code>erase<\/code> requires two iterators which I provided by the algorithm <code>std::remove_if<\/code>.<\/p>\n<p>Let&#8217;s see what the new functions <code>erase<\/code> and <code>erase_if<\/code> mean in practice. The following program erases elements for a few containers.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ eraseCpp20.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;numeric&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;deque&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;list&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont<span style=\"color: #555555;\">&gt;                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/span><\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> eraseVal(Cont<span style=\"color: #555555;\">&amp;<\/span> cont, <span style=\"color: #007788; font-weight: bold;\">int<\/span> val) {\r\n    std<span style=\"color: #555555;\">::<\/span>erase(cont, val);\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Pred<span style=\"color: #555555;\">&gt;                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (8)<\/span><\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> erasePredicate(Cont<span style=\"color: #555555;\">&amp;<\/span> cont, Pred pred) {\r\n    std<span style=\"color: #555555;\">::<\/span>erase_if(cont, pred);\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> printContainer(Cont<span style=\"color: #555555;\">&amp;<\/span> cont) {\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> c<span style=\"color: #555555;\">:<\/span> cont) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> c <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont<span style=\"color: #555555;\">&gt;                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span><\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> doAll(Cont<span style=\"color: #555555;\">&amp;<\/span> cont) {\r\n    printContainer(cont);\r\n    eraseVal(cont, <span style=\"color: #ff6600;\">5<\/span>);\r\n    printContainer(cont);\r\n    erasePredicate(cont, [](<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">&gt;=<\/span> <span style=\"color: #ff6600;\">3<\/span>; } );\r\n    printContainer(cont);\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>string str{<span style=\"color: #cc3300;\">\"A Sentence with an E.\"<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"str: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> str <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>erase(str, <span style=\"color: #cc3300;\">'e'<\/span>);                                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"str: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> str <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>erase_if( str, [](<span style=\"color: #007788; font-weight: bold;\">char<\/span> c){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>isupper(c); });  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"str: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> str <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">std::vector \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>vector vec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    doAll(vec);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">std::deque \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>deque deq{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    doAll(deq);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">std::list\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>list lst{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    doAll(lst);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line (1) erases all character <code>e<\/code> from the given string<code> str.<\/code> Line (2) applies the lambda expression to the same string and erases all upper-case letters.<\/p>\n<p>In the remaining program, elements of the sequence containers<code> std::vecto<\/code>r (line 3),<code> std::deque<\/code> (line 4), and<code> std::list<\/code> (line 5) are erased. On each container, the function template <code>doAll<\/code> (line 6) is applied. <code>doAll<\/code> erases the element 5 and all elements greater than 3. The function template <code>erase<\/code> (line 7) uses the new function <code>erase<\/code> , and the function template <code>erasePredicate<\/code> (line 8) uses the new function <code>erase_if<\/code>.<\/p>\n<p>Thanks to the Microsoft Compiler, here is the output of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5992\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseCpp20.PNG\" alt=\"eraseCpp20\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"378\" height=\"456\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseCpp20.PNG 378w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/eraseCpp20-249x300.png 249w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/p>\n<p>The new functions <code>erase<\/code> and <code>erase_if<\/code> can be applied to all containers of the Standard Template Library. This does not hold for the following convenience function <code>contains<\/code>.<\/p>\n<h2>Checking the Existence of an Element in an Associative Container<\/h2>\n<p>Thanks to the functions <code>contains<\/code>, you can quickly check if an element exists in an associative container.<\/p>\n<p>Stopp, you may say, we can already do this with <span style=\"font-family: courier new, courier;\">find<\/span> or <span style=\"font-family: courier new, courier;\">count.&nbsp;<\/span><\/p>\n<p>No, both functions are not beginners friendly and have their downsides.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ checkExistens.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;set&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>set mySet{<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">1<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (mySet.find(<span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #555555;\">!=<\/span> mySet.end()) {    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"2 inside\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>multiset myMultiSet{<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (myMultiSet.count(<span style=\"color: #ff6600;\">2<\/span>)) {            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"2 inside\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    } \r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The functions produce the expected result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5993\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/checkExistens.PNG\" alt=\"checkExistens\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"378\" height=\"182\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/checkExistens.PNG 378w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/checkExistens-300x144.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/p>\n<p>Here are the issues with both calls. The <code>find<\/code> call inline (1) is too lengthy. The same argumentation holds for the <code>count<\/code> call in line (2). The <code>count<\/code>&nbsp;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,<code> myMultiSet.count(2)<\/code> returned 2.<\/p>\n<p>On the contrary, the contains member function in C++20 is quite convenient.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ containsElement.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;set&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;map&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;unordered_set&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;unordered_map&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> AssozCont<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> containsElement5(<span style=\"color: #006699; font-weight: bold;\">const<\/span> AssozCont<span style=\"color: #555555;\">&amp;<\/span> assozCont) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> assozCont.contains(<span style=\"color: #ff6600;\">5<\/span>);\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>set<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> mySet{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"containsElement5(mySet): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> containsElement5(mySet);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>unordered_set<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myUnordSet{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"containsElement5(myUnordSet): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> containsElement5(myUnordSet);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>map<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myMap{ {<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #cc3300;\">\"red\"<\/span>}, {<span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #cc3300;\">\"blue\"<\/span>}, {<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #cc3300;\">\"green\"<\/span>} };\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"containsElement5(myMap): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> containsElement5(myMap);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>unordered_map<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myUnordMap{ {<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #cc3300;\">\"red\"<\/span>}, {<span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #cc3300;\">\"blue\"<\/span>}, {<span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #cc3300;\">\"green\"<\/span>} };\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"containsElement5(myUnordMap): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> containsElement5(myUnordMap);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There is not much to add to this example. The function template <code>containsElement5<\/code> returns <code>true<\/code> if the associative container contains the key 5. In my example, I only used the associative containers<code> std::set<\/code>, <code>std::unordered_set<\/code>, <code>std::map<\/code>, and <code>std::unordered_set<\/code> which can not have a key more than once.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5994\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/containsElement.PNG\" alt=\"containsElement\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"405\" height=\"190\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/containsElement.PNG 405w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/09\/containsElement-300x141.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The convenience functions go on in my<a href=\"https:\/\/www.modernescpp.com\/index.php\/std-format-in-c-20\"> next post<\/a>. With C++20, you can calculate the midpoint of two values, check if a <code>std::string<\/code> start or ends with a substring, and create callables with<code> std::bind_front<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[],"class_list":["post-5995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5995"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5995\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}