Apr 05 2010

Finding an element in a list

Category: .codeAmit Bahree @ 3:17 pm

Often you need to search through an array or list to find a specific element and of course you need this search to be as fast and efficient as possible. One of the best ways to do this is using a binary predicate function.

A binary function is a function object (which are also called Functors) and is any object which can be called as if it was a function. Depending on your language and platform of choice, Function objects are also known as callback functions, function pointers and delegates (.NET). Generally, there are three types of function objects:

  1. Generators – function with no arguments
  2. Unary Functions – function with one argument
  3. Binary Functions – functions with two arguments

A function object which takes one parameter (i.e. unary function) and returns a bool are treated as a special case and are  called Predicate functions.

How do we use it? Say we have a simple data structure called ContactData to represent a Contact in an Address book as shown in the code snippet below. We also define a predicate function called FindAContact. Now we need to use this predicate function and define another function called  findContact. The findContact function in turn uses find_if.  find_if takes three parameters, the start of the iterator, the last element and the predicate to use. It returns the first iterator it finds in a given range for which the predicate holds. If no matches are found then  the last element in the iterator is returned.

We also need to ensure we have the relevant includes for this to compile and link properly hence include’s below.

The code snippet below shows all that we have discussed.

#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

//Simple data structure
struct ContactData {
	string name;
	string addr1;
	string addr2;
	string addr3;
	string city;
	string postcode;
	string country;
	int workPhone;
	int homePhone;
	int mobilePhone;
	string workEmail;
	string homeEmail;
};

//I am lazy, create a typedef for the vector
typedef vector<ContactData> ContactDataArray;

// predicate function for rapidly searching the Contact data array
struct FindAContact: public std::binary_function<ContactData, std::string, bool>
{
	bool operator() (const ContactData &contact, const string &name) const
	{
		return (contact.name == name);
	}
};

//If a contact is find it returns that; else returns the iterator's last element
ContactData Contact::findContact(string name)
{
	ContactDataArray::iterator it = find_if(addressBook.begin(),
						addressBook.end(),
						std::bind2nd(FindAContact(), name)
					);

	return *it;
}
Share
Similar posts to check out:
  • May 26, 2011 -- Troubleshooting WCF Performance – Part 1 (0)
    More related details on Dustin's post - WCF scales up slowly with bursts of work....
  • May 23, 2011 -- PowerShell script to kill named processes (0)
    There are times when you need to kill a number of processes in one-go like today when Chrome crashed a few times hanging all the running instances – next time Google says, one tab cannot bring down all of them – send them my way :). For such times, a PowerShell script is all you need. I wrote up a simple one which takes the process name as input and then kills all the processes which match that name. [sourcecode lang="PowerShell" toolbar="true"] #Script is not signed, so need this. Set-...
  • March 4, 2011 -- Twitter Trends (0)
    I was excited to find that Twitter had a JSON (Javascript Object Notation) endpoint for the current trending topics and decided to write a simple consumer which can read this and then spit it out in a simple console. And JSON being so simple and more or less “universal” meant that there are multiple implementations for .NET. Of course if you got lots of bandwidth you can roll out your own parser. I ended up using Json.NET, which in addition to being OpenSource is also one of the most robust u...

Tags:

Leave a Reply

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes