{"id":2439,"date":"2016-05-20T11:56:16","date_gmt":"2016-05-20T15:56:16","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=2439"},"modified":"2021-12-18T05:41:21","modified_gmt":"2021-12-18T10:41:21","slug":"non-modifying-algorithms","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/","title":{"rendered":"The C++ Standard Template Library Non-Modifying Algorithms – Part 7"},"content":{"rendered":"\n

Welcome again to the seventh part of our continuing series on the C++ Standard Template Library<\/a>. This chapter comes after a couple of months after part six where we looked at the introduction to algorithms as we also touched on Iterators<\/a>. Today we will be taking a brief look at non-modifying algorithms.<\/p>\n\n\n\n

Again if you have missed the entirety of this series and are not familiar with the C++<\/a> STL then I would recommend you go back to the beginning and follow through on the entire series starting with the overview here<\/a>.<\/p>\n\n\n\n

Why Use Non-Modifying Algorithms<\/h2>\n\n\n\n

The simple answer is because they give you the stability, efficiency, security, and simplicity you get out of using the STL while programming<\/a>.<\/p>\n\n\n\n

It is good practice to use the C++ STL wherever possible in your code. The library<\/a> is very mature, efficient, and safe and therefore you need to give priority to STL wherever there is an STL solution to the problem you are trying to solve. The same is true with other libraries and that is exactly what a library is there for in the first place.<\/p>\n\n\n\n

What are Non-Modifying Algorithms?<\/h2>\n\n\n\n

These are basically algorithms that do not modify the data on which they operate. In other terms, they are read-only functions that return a value based on a read and check on the underlying data. For instance, you could have a function that simply counts elements or you might want to know the greatest element in a range.<\/p>\n\n\n\n

There are several ways to accomplish the trivial tasks mentioned above. You could write your own function or go with existing algorithms. Of course, the ideal choice here should be algorithms and there is just one simple way to get you familiar with them. The best way to illustrate this, of course, is through some examples.<\/p>\n\n\n\n

We will illustrate the use of a few non-modifying algorithms then leave you to brush through the C++ documentation to get the list of algorithms at your fingertips.<\/p>\n\n\n\n

Counting Algorithm<\/h2>\n\n\n\n

Assuming we want to get the number of times a certain value appears in an array. This example would accomplish that:<\/p>\n\n\n\n

...\nvector<int> v = {2,5,3,6,4,8,5,9,6,1,7,3,12,14,25};\nint a = count(v.begin(), v.end(), 3); \n\/\/ The value of "a" is 2\n\n\/\/Below we use a lambda and count_if to see how many elements \n\/\/are greater than 10\nint b = count_if(v.begin(), v.end(),[](int x){return x > 10;});\n\/\/ The value of "b" above is 3<\/code><\/pre>\n\n\n\n

How about Getting the Maximums and Minimums from an array<\/h2>\n\n\n\n

We start off by getting the highest value in the array.<\/p>\n\n\n\n

...\nvector<int> v = {2,5,3,6,4,8,5,9,6,1,7,3,12,14,25};\nint a = max_element(v.begin(), v.end());\n\/\/ The value of "a" above is 25<\/code><\/pre>\n\n\n\n

Likewise, we can find the smallest value in the array:<\/p>\n\n\n\n

...\nvector<int> v = {2,5,3,6,4,8,5,9,6,1,7,3,12,14,25};\nint a = min_element(v.begin(), v.end());\n\/\/ The value of "a" above is 1<\/code><\/pre>\n\n\n\n

How about getting the maximum and minimum values in an array in a single statement.<\/p>\n\n\n\n

...\nvector<int> v = {2,5,3,6,4,8,5,9,6,1,7,3,12,14,25};\nint a = minmax_element(v.begin(), v.end());\n\/\/ The value of "a.fist" above is 1\n\/\/ The value of "a.second" above is 25<\/code><\/pre>\n\n\n\n

That’s it for this part. All other non-modifying algorithms work in a simple and consistent manner as those illustrated here. In the next chapter of this C++ STL series, we will be taking a brief look at the modifying algorithms in part 8 of the Standard Template Library tutorial series<\/a>. We will focus more on showing illustrations of how they work within the C++ Standard Template Libraries.<\/p>\n","protected":false},"excerpt":{"rendered":"

Welcome again to the seventh part of our continuing series on the C++ Standard Template Library. This chapter comes after a couple of months after part six where we looked…<\/p>\n","protected":false},"author":1,"featured_media":2440,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,16,20],"tags":[47,111,350,471,544,545,563,591],"yoast_head":"\nNon-Modifying Algorithms, Part 7 of the C++ STL Series<\/title>\n<meta name=\"description\" content=\"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Non-Modifying Algorithms, Part 7 of the C++ STL Series\" \/>\n<meta property=\"og:description\" content=\"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-20T15:56:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-18T10:41:21+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"The C++ Standard Template Library Non-Modifying Algorithms – Part 7\",\"datePublished\":\"2016-05-20T15:56:16+00:00\",\"dateModified\":\"2021-12-18T10:41:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\"},\"wordCount\":476,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg\",\"keywords\":[\"Algorithms\",\"C++\",\"Libraries\",\"Programming\",\"Software development\",\"Software Engineering\",\"STL\",\"Tools\"],\"articleSection\":[\"Articles\",\"Guides\",\"Technology\",\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\",\"url\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\",\"name\":\"Non-Modifying Algorithms, Part 7 of the C++ STL Series\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg\",\"datePublished\":\"2016-05-20T15:56:16+00:00\",\"dateModified\":\"2021-12-18T10:41:21+00:00\",\"description\":\"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg\",\"width\":1200,\"height\":630,\"caption\":\"C++ STL Non-Modifying Algorithms\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The C++ Standard Template Library Non-Modifying Algorithms – Part 7\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Non-Modifying Algorithms, Part 7 of the C++ STL Series","description":"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/","og_locale":"en_US","og_type":"article","og_title":"Non-Modifying Algorithms, Part 7 of the C++ STL Series","og_description":"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming","og_url":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2016-05-20T15:56:16+00:00","article_modified_time":"2021-12-18T10:41:21+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"The C++ Standard Template Library Non-Modifying Algorithms – Part 7","datePublished":"2016-05-20T15:56:16+00:00","dateModified":"2021-12-18T10:41:21+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/"},"wordCount":476,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg","keywords":["Algorithms","C++","Libraries","Programming","Software development","Software Engineering","STL","Tools"],"articleSection":["Articles","Guides","Technology","Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/non-modifying-algorithms\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/","url":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/","name":"Non-Modifying Algorithms, Part 7 of the C++ STL Series","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg","datePublished":"2016-05-20T15:56:16+00:00","dateModified":"2021-12-18T10:41:21+00:00","description":"In the 7th part of our C++ Standard Template Library Series we discuss the non-modifying algorithms and how they are applied while programming","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/non-modifying-algorithms\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2016\/05\/stl-non-modifying-algorithm.jpg","width":1200,"height":630,"caption":"C++ STL Non-Modifying Algorithms"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/non-modifying-algorithms\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"The C++ Standard Template Library Non-Modifying Algorithms – Part 7"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/2439"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=2439"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/2439\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/2440"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=2439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=2439"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=2439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}