waqqasfarooq.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Useful Code Snippets STL bind and find algorithm example


Bind and find (C++ std bind2nd and find_if example)

E-mail Print PDF
User Rating: / 12
PoorBest 

STL provides us with very useful algorithms and utilities. You start enjoying the usefulness of programming when you reap the benefits of these utils. In this article I will try to show you the usefulness of the std::bind2nd function. The basic idea behind bind2nd (or bind1st) is that it will take a binary function along with one of the parameters and bind it in a function object. This function object will be then a unary function. If all this sounds confusing then don't worry too much about it. You should read these first few lines again after you go through the example and hopefully then this will all make sense.

I have a created an example to illustrate how you can use std::find_if and std::bind2nd to search through a STL collection. I've tries to keep the example simple. Please let me know if you have trouble understanding the code. In this example we will create a collection of Person and we will search through this collection.

Example Description:

I want to create a collection of Person. A person is an object that has the following 3 fields:

  1. name
  2. age
  3. gender
In the Person collection I want to add a bunch of Person objects. I will just make up a few persons by setting the name, age and gender for each. Note: Persons I add in the collection are fictitious. Next, I will show you how you can use STL to print out the following results from the collection:
  1. The list of persons after (and including) the first "Female" person
  2. The list of persons with only "Male" persons
  3. The list of persons who are of age 34

Solution:


So, lets go ahead and translate the above problem into computer understandable language.

Right off the bat, we see that we need to define a struct or class that contains the person's fields. For out simple example we will use this object to denote a person. In terms of coding, a person should be defined as follows:

Person Definition
View source
  1. /**
  2.  * Struct that denotes a Person
  3.  */
  4. struct Person
  5. {
  6. std::string m_name;
  7. unsigned int m_age;
  8. std::string m_gender;
  9.  
  10.  
  11. // Constructor with initializers
  12. Person(const std::string name,
  13. unsigned int age,
  14. const std::string& gender)
  15. : m_name(name)
  16. , m_age(age)
  17. , m_gender(gender)
  18. {
  19. // do nothing
  20. }
  21.  
  22. private:
  23. Person();
  24. };


I am also adding the following function just so that it's easier for me to print a person object. I use the concept of operator overloading in the following code:

Overloading operator<<

View source
  1. /**
  2.  * Overloading operator<< so that its easier to
  3.  * print the Person object
  4.  */
  5. std::ostream& operator<<(std::ostream& stream, const Person& person)
  6. {
  7. // format the output
  8. stream << "Name: " << person.m_name << ",\t\t"
  9. << "Age: " << person.m_age << ",\t"
  10. << "Gender: " << person.m_gender << std::endl;
  11.  
  12. return stream;
  13. }


So far, we have translated the Person described in the problem statement into a computer understandable Person type. We also have added a utility function to make it easier for us to print a person object on the console by overloading the operator<<. Now, we will be using function predicates to search through the STL collection, therefore we should add the predicate functions as well. The following code shows how to add predicates for gender and age search:

alt

Both predicate structs inherit from the std::binary_function type. This is needed for STL bind2nd.

Also, for our convenience we will typedef the collection as follows:

alt


The typedef is done so that we don't have to type std::vector each time we want to refer to the Persons collection in the code.


Now that we have all are utility functions and struct in place, we have to go ahead and work on populating the Persons collection and the searching through them using the STL find_if algorithm. The code is given as follows:

Populate Persons

View source
  1. // create a collection of persons
  2. Persons persons;
  3.  
  4. // populate persons collection with many Person objects
  5. persons.push_back(Person("Calvin", 34, "Male"));
  6. persons.push_back(Person("Klein", 29, "Male"));
  7. persons.push_back(Person("Lady", 29, "Female"));
  8. persons.push_back(Person("Gaga", 29, "Male"));
  9. persons.push_back(Person("Kenneth", 30, "Male"));
  10. persons.push_back(Person("Cole", 34, "Female"));
  11. persons.push_back(Person("Ralph", 30, "Male"));
  12. persons.push_back(Person("Lauren", 34, "Male"));
  13. persons.push_back(Person("Burberry", 34, "Female"));
  14. persons.push_back(Person("Giorgio", 29, "Male"));
  15. persons.push_back(Person("Anne", 34, "Female"));
  16. persons.push_back(Person("Taylor", 30, "Male"));
  17.  


alt


alt


alt


Appendix A - Downloads

 

Comments  

 
0 #1 Faraz Shaikh 2010-11-25 19:51
couldnt do it without this reference Smile