C++ vector find_if usage with lambda functions

July 17, 2016

Reading time ~1 minute

C++ language’s standard vector has a function called “find_if” provides search functionality depends on a boolean return typed function. Let’s say you have an integer vector and trying to find an even number :

#include <iostream>
#include <algorithm>
#include <vector>

bool IsEven (int i) {
	return ((i%2)==0);
}

int main () {
	std::vector<int> numberVector;

	numberVector.push_back(1);
	numberVector.push_back(3);
	numberVector.push_back(5);
	numberVector.push_back(6);
	numberVector.push_back(7);

	std::vector<int>::iterator it = std::find_if (numberVector.begin(), numberVector.end(), IsEven);
	printf("First even number : %d", *it);

	return 0;
}

But what if you want to pass an additional parameter to your search function and use lambda functions. Take a look to the following code snippet. We have a Popup object and a lambda function defines a search condition depending on name of the popup.

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include "Popup.h"

int main () {

	std::vector<Popup*> popups;
	
	popups.push_back(new Popup("name1", "message1"));
	popups.push_back(new Popup("name2", "message2"));
	popups.push_back(new Popup("name3", "message3"));
	
	const char* popupName = "name2";

	std::vector<Popup*>::iterator it = std::find_if(popups.begin(), popups.end(), [popupName](Popup* popup)->bool {
		return strcmp(popup->getName(), popupName) == 0;
	});

	return 0;
}

It is so simple, lambda function’s scope holds the local constant “popupName”. Finally iterator “it” holds the result :)

Depending on an aar library includes transitive dependencies in a maven project

Depending on an aar library includes transitive dependencies in a maven project Continue reading