cpp find_if code example

Example 1: find_if c++ example

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  vector<int> myvec {2, 5, 6, 10, 56, 7, 48, 89};
  vector<int>::iterator gt10 = find_if(myvec.begin(), myvec.end(), [](int x){return x>10;}); // >= C++11
  cout << "First item >   10: " << *gt10  << endl;
  
    //check if pointer points to myvec.end()
  if(gt10 != myvec.end()) {
    cout << "nf points to: " << *gt10 << endl;
  }
  else {
    cout << "item not found" << endl;
  }

  return 0;
}

Example 2: c++ find with predicat

if(find_if(table.begin(), table.end(), [&new_id](const entry &arg) { 
                                           return arg.first == new_id; }) != ...)

Tags:

Cpp Example