- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Functors in C++
The functors are the function objects in C++. The functor allows an instance object of some class to be called as if it were an ordinary function. Let us consider a function that takes one argument. We can use this function as function object to do some task on a set of data.
Example Code
#include <iostream> #include <algorithm> using namespace std; int square(int x) { return x*x; //return square of x } int main() { int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; transform(data, data+10, data, square); for (int i = 0; i<10; i++) cout << data[i] << endl; }
Output
0 1 4 9 16 25 36 49 64 81
Advertisements
To Continue Learning Please Login
Login with Google