Non-Modifying Algorithms

#include <algorithm>

for_each

#include <algorithm>
...
template <typename T>
class ContInfo{
public:
  void operator()(T t){
    num++;
    sum+= t;
  }
  int getSum() const{ return sum; }
  int getSize() const{ return num; }
  double getMean() const{
    return static_cast<double>(sum)/static_cast<double>(num);
  }
private:
  T sum{0};
  int num{0};
};

std::vector<double> myVec{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
auto vecInfo= std::for_each(myVec.begin(), myVec.end(), ContInfo<double>());

std::cout << vecInfo.getSum() << std::endl;    // 49
std::cout << vecInfo.getSize() << std::endl;   // 9
std::cout << vecInfo.getMean() << std::endl;   // 5.5

std::array<int, 100> myArr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto arrInfo= std::for_each(myArr.begin(), myArr.end(), ContInfo<int>());

std::cout << arrInfo.getSum() << std::endl;    // 55
std::cout << arrInfo.getSize() << std::endl;   // 100
std::cout << arrInfo.getMean() << std::endl;   // 0.55

Search Elements

std::find, std::find_if, std::find_if_not, std::find_of, and std::adjacent_find

bool isVowel(char c){
  string myVowels{"aeiouäöü"};
  set<char> vowels(myVowels.begin(), myVowels.end());
  return (vowels.find(c) != vowels.end());
}

list<char> myCha{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int cha[]= {'A', 'B', 'C'};

cout << *find(myCha.begin(), myCha.end(), 'g');             // g
cout << *find_if(myCha.begin(), myCha.end(), isVowel);      // a
cout << *find_if_not(myCha.begin(), myCha.end(), isVowel);  // b

auto iter= find_first_of(myCha.begin(), myCha.end(), cha, cha + 3);
if (iter == myCha.end()) cout << "None of A, B or C.";      // None of A, B or C.
auto iter2= find_first_of(myCha.begin(), myCha.end(), cha, cha+3, 
            [](char a, char b){ return toupper(a) == toupper(b); });

if (iter2 != myCha.end()) cout << *iter2;                   // a
auto iter3= adjacent_find(myCha.begin(), myCha.end());
if (iter3 == myCha.end()) cout << "No same adjacent chars."; 
                                        // No same adjacent chars.

auto iter4= adjacent_find(myCha.begin(), myCha.end(),
            [](char a, char b){ return isVowel(a) == isVowel(b); });
if (iter4 != myCha.end()) cout << *iter4;                   // b 
struct A{
    int v;
    bool operator==(A other){
        return other.v==v;
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc,argv);
    QList<A> list{{1},{2}};
    auto it =std::find(list.begin(),list.end(),A{2});
    if(it==list.end())
        qDebug()<<"can not found it";
    else
        qDebug()<<(*it).v;
    return a.exec();
}
struct A{
    int v;
    bool operator==(A other){
        return other.v==v;
    }
};

struct comparator{
    bool operator()( A& a1, A& a2){
        qDebug()<<a1.v<<a2.v;
        return false;
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication a(argc,argv);
    QList<A> list{{1},{2},{3},{4},{5}};
    auto it =std::adjacent_find(list.begin(),list.end(),comparator());//(1,2),(2,3),(3,4),(4,5)
    return a.exec();
}

Count Elements

std::count, and std::count_if

std::string str{"abcdabAAAaefaBqeaBCQEaadsfdewAAQAaafbd"};
std::cout << std::count(str.begin(), str.end(), 'a');              // 9
std::cout << std::count_if(str.begin(), str.end(),              
                           [](char a){ return std::isupper(a); }); // 12

Check Conditions on Ranges

std::all_of, std::any_of, and std::none_of

auto even= [](int i){ return i%2; };
std::vector<int> myVec{1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << std::any_of(myVec.begin(), myVec.end(), even);  // true
std::cout << std::all_of(myVec.begin(), myVec.end(), even);  // false
std::cout << std::none_of(myVec.begin(), myVec.end(), even); // false

Compare Ranges

equal , mismatch

string str1{"Only For Testing Purpose."};
string str2{"only for testing purpose."};
cout << equal(str1.begin(), str1.end(), str2.begin());  // false
cout << equal(str1.begin(), str1.end(), str2.begin(),
              [](char c1, char c2){ return toupper(c1) == toupper(c2);} ); 
			                                          // true
str1= {"Only for testing Purpose."};
str2= {"Only for testing purpose."};
auto pair= mismatch(str1.begin(), str1.end(), str2.begin());
if (pair.first != str1.end()){
  cout << distance(str1.begin(), pair.first)
       << "at (" << *pair.first << "," << *pair.second << ")";  // 17 at (P,p)
}
auto pair2= mismatch(str1.begin(), str1.end(), str2.begin(),
                     [](char c1, char c2){ return toupper(c1) == toupper(c2); });
if (pair2.first == str1.end()){
  cout << "str1 and str2 are equal";  // str1 and str2 are equal
}

Search for Ranges within Ranges

search : Searches the second range in the first one and returns the position. Starts at the beginning

find_end : Searches the second range in the first one and returns the positions. Starts at the end

search_n : Searches count consecutive values in the first range:

std::array<int, 10> arr1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::array<int, 5> arr2{3, 4, -5, 6, 7};

auto fwdIt= search(arr1.begin(), arr1.end(), arr2.begin(), arr2.end());
if (fwdIt == arr1.end()) std::cout << "arr2 not in arr1.";  // arr2 not in arr1.

auto fwdIt2= search(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(),
                    [](int a, int b){ return std::abs(a) == std::abs(b); });
if (fwdIt2 != arr1.end()) std::cout << "arr2 at position "
                    << std::distance(arr1.begin(), fwdIt2) << " in arr1."; 
                                                  // arr2 at position 3 in arr1.

parallel Algorithms

#include <iostream>
#include <vector>
#include <execution>
#include <algorithm>
#include <stdlib.h>
#include<time.h>
#include<chrono>

int main(int argc,const char* argv[]){
    std::srand(time(nullptr));
    std::vector<double> values;
    for(int i=0;i<=9999999;i++){
        values.push_back(rand()%9999);
    }
    std::chrono::time_point<std::chrono::system_clock> tf=std::chrono::system_clock::now();
    std::for_each(std::execution::par_unseq,values.begin(),values.end(),[](double& el){el=el/12.0+120.0;});
    int res=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()-tf).count();
    std::cout<<res;


    return 0;
}

to use the execution library with Linux you have to install libtbb-dev and add -ltbb flag to the compiler