Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Sep 19, 2006



Application Security Testing: An Integral Part of DevOps

Object Functions in Use

An object function is a regular C++ object whose class defines the () operator. An object with a () operator can be called like a function:

CFoo foo; // just an object whose class has operator() defined
foo(); // like a function; makes operator() work
You can use an object function to find the first occurence of a string started by a given symbol in a vector:

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

class starts {
	char ch;
	starts(char c) : ch(c) {}
	bool operator() (const string &s) {
		return s.size() && s[0] == ch;

int main()
	vector<string> vec(3); // vector of three elements
	vec[0] = "sir";
	vec[1] = "zebra";
	vec[2] = "home";
	vector<string>::iterator iter =
		find_if(vec.begin(), vec.end(), starts('z'));
	cout << *iter << "\n";
	return 0;
Arman Sahakyan
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date