May 2015
Intermediate to advanced
572 pages
9h 52m
English
CHAPTER 8
![]()
Functors
This chapter focuses on several techniques that help when writing (or when not writing) functors.
Most STL algorithms require compile-time function objects and this usually requires some manual coding:
struct Person{ unsigned int age; std::string home_address; double salary() const;};std::vector<Person> data;std::sort(data.begin(), data.end(), /* by age */ );std::partition(data.begin(), data.end(), /* by salary */ );
If you can modify Person, sometimes an elegant and quick solution is to write a public static member function and a member functor. This simultaneously attains the maximum efficiency and control, as your ...