ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

CPP STL: Non modifying sequence operation algorithms

prodevelopertutorial March 28, 2020
Below are the operations that are used to perform on STL containers.
Most of the functions are function template. Hence they can be used to work with primitive and also user defined data types.
To use these algorithms, below header should be included:

#include <algorithm>
all_of   : Test condition on all elements in range [C++ 11]
any_of : Test if any element in range fulfills condition  [C++ 11]
none_of   : Test if no elements fulfill condition  [C++ 11]
for_each : Apply function to range
find : Find value in range
find_if : Find element in range
find_if_not : Find element in range [C++ 11]
find_end : Find last subsequence in range
find_first_of : Find element from set in range
adjacent_find : Find equal adjacent elements in range
count : Count appearances of value in range
count_if : Return number of elements in range satisfying condition
mismatch : Return first position where two ranges differ
equal : Test whether the elements in two ranges are equal
is_permutation  :Test whether range is permutation of another  [C++ 11]
search : Search range for subsequence
search_n : Search range for elements
In below series of examples, we shall see operations:
Example 1: Usage of all_of, any_of, for_each

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>    // std::all_of

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

void myfunction (int i)  // function to print
{ 
  std::cout << ' ' << i;
}


int main () 
{
  std::vector<int> foo = {3,5,7,11,13,17,19,23};

  // below code will check if the elements in foo are all odd numbers
  if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
    std::cout << "All the elements are odd numbers by using all_of.\n";
    
  // below code will check if any one of the element is a negative number
  std::array<int,7> myarr = {0,1,1,3,3,5,-5};
  if ( std::any_of(myarr.begin(), myarr.end(), [](int i){return i<0;}) )
    std::cout << "There are negative elements in the range.\n";

  std::cout << "\nDisplaying all the elements in the vector \"foo\" by using for_each\n";
  for_each (foo.begin(), foo.end(), myfunction);
  std::cout << '\n';

  return 0;
}

All the elements are odd numbers by using all_of.
There are negative elements in the range.

Displaying all the elements in the vector "foo" by using for_each
 3 5 7 11 13 17 19 23

Example 2: Usage of find, find_if, count, count_if

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>    // std::all_of

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

bool IsOdd (int i) 
{
  return ((i%2)==1);
}

int main () 
{
  std::vector<int> myvector = {3,5,7,11,13,17,19,23};
  std::vector<int>::iterator it;

    // check if 30 is present by using find()
  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end())
    std::cout << "Element 30 found in myvector: " << *it << '\n';
  else
    std::cout << "Element 30 not found in myvector\n";
    
  //check if there is an odd value by using find_if
  it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "The first odd value is " << *it << '\n';
  
  int mycount = std::count (myvector.begin(), myvector.end(), 5);
  std::cout << "5 appears " << mycount  << " times.\n";

  mycount = count_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "myvector contains " << mycount  << " odd values.\n";
  return 0;
}

Output:

Element 30 not found in myvector
The first odd value is 3
5 appears 1 times.
myvector contains 8 odd values.


 

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT