Call operators and function objects

Source: Internet
Author: User

P451-p467

Do not go to bed after reading Part 3 today ~!!!!!!!!!!!!!!!!!!Tomorrow ..

Simply put, the object can be viewed as a function.

// Function object, # include "head. H" struct absint {int operator () (INT Val) {// returns the absolute value of Val <0? -Val: Val ;}}; int main () {int I =-33; absint absobj; unsigned int UI = absobj (I); STD :: cout <UI <STD: Endl ;}

Pe14_31

// If-is complete,-otherwise. # Include "head. H" struct func {int operator () (bool B, int val1, int val2) {// returns the absolute value of return B? Val1: val2 ;}}; int main () {int A = 33, B = 44; func func1; bool open; // status mark open = 1; unsigned int UI = func1 (open, a, B); STD: cout <UI <STD: Endl; open = 0; UI = func1 (open,, b); STD: cout <UI <STD: Endl ;}

Exercise 14.32

Q: How many operations can an overloaded function call operator accept?

A: countless ?! It seems to be .... What is the purpose of this question (Mark)

Int operator () (bool B, int val1, int val2 ,...); // declare unsigned int UI = func1 (open, a, B, a, a); // call

========================================================== ========================================================== ========================================================== ==========

Next we will discuss the shortcomings of the previously impressive gt6 (greater than 6) function: the code (function) reusability (I don't know the proper vocabulary) is too low, 6 is written in. To change the code, you can only change the code.

bool GT6(const string &s){return s.size() >= 6;}

Review: supports and does not support generic algorithms, iterator, inserter, and various capacity algorithms. Capacity features and sequences can be read randomly and inserted before insertion...

In addition, there are several functions in string that are skipped, such as append and merge.

Vector <string >:: size_tyoe WC = count_if (words. begin (), words. end (), gt6); // The counting algorithm provided by the standard library algorithm. The third is conditional judgment, which returns the bool type. gt6 has a title called the predicate function.

// Use gt_cls to compensate for gt6 defects # include "head. H "class gt_cls {public: gt_cls (size_t val = 0): bound (VAL) {}// the constructor receives a bound value with the default real parameter 0 bool operator () (const STD: string & S) {// judge return s for the input string object. size ()> = bound;} PRIVATE: STD: String: size_type bound;}; int main () {gt_cls gt_3 (3); gt_cls gt_4 (4 ); gt_cls gt_5 (5); STD: String str1 ("he"); STD: String str2 ("hell"); STD: String str3 ("world "); STD: vector <STD: String> words; words. push _ Back (str1); words. push_back (str2); words. push_back (str3); New gt_cls (2); gt_cls (3); // This is difficult to understand. It is the process of Initializing an object. How to Use It depends on count_if () implementation, // the object that I passed in should be a just-initialized object. The object function he called is indeed added with a string parameter. // That is to say, the input is a function, and the parameters entered by the handler do not care, (store this function location through pointers and the like) // no matter what the input formula is, count_if () remembers this form and considers it a function, each call to gt_cls (Str .); STD: cout <STD: count_if (words. begin (), words. end (), gt_cls (3) <"words 3 characters or longer" <STD: Endl; STD: cout <STD: count_ I F (words. begin (), words. end (), gt_cls (4) <"words 4 characters or longer" <STD: Endl; STD: cout <STD: count_if (words. begin (), words. end (), gt_cls (5) <"words 5 characters or longer" <STD: Endl; // The input variable is more useful, complete the batch task for (size_t I = 1; I! = 11; ++ I) STD: cout <count_if (words. begin (), words. end (), gt_cls (I) <"Words" <I <"characters or longer" <STD: Endl; // You can also calculate STD: cout <count_if (words. begin (), words. end (), gt_cls (3)-count_if (words. begin (), words. end (), gt_cls (6) <STD: Endl ;}

Pe14_33

// Use the standard library algorithm and the gt_cls class to find the first element larger than the specified value STD: cout <* (find_if (words. begin (), words. end (), gt_cls (2) <STD: Endl; STD: cout <* (find_if (words. begin (), words. end (), gt_cls (4) <STD: Endl; STD: cout <* (find_if (words. begin (), words. end (), gt_cls (5) <STD: Endl ;}

Pe14_34

// Determine whether the values are equal. Use the standard library algorithm to replace the specified value. If finde_if () is used, replace # include "head. H "class word_equal {public: word_equal (const STD: String STR =" "): Word (STR) {}// the constructor receives a boundary value, default real parameter 0 bool operator () (const STD: string & S) {// judge the input string object return S = word;} PRIVATE: STD :: string word;}; int main () {STD: String str1 ("he"); STD: String str2 ("hell"); STD :: string str3 ("world"); STD: vector <STD: String> words; words. push_back (str1); words. push _ Back (str2); words. push_back (str3); words. push_back (str1); For (STD: vector <STD: String >:: iterator iter = words. begin (); iter! = Words. end (); ITER ++) STD: cout <* ITER <"\ t"; // replace STD: string search ("he "); while (STD: count_if (words. begin (), words. end (), word_equal (Search) {// use count_if () to determine whether the corresponding object exists * (STD: find_if (words. begin (), words. end (), word_equal (Search) = "Holy God ~! "; // Use find_if () to change one by one} STD: cout <STD: Endl <" after change: "<STD: Endl; For (STD :: vector <STD: String >:: iterator iter = words. begin (); iter! = Words. End (); ITER ++) STD: cout <* ITER <"\ t ";}

Pe14_35

// Replace gt6 with an object. // you may not be familiar with the requirement. test whether the length of the given String object matches its boundary. (If it seems nonsense, you just need to find the value between 1 and 10) // The number of words in the report input is between 1 and 10. // the original program calculates the number of words in the report. This is because the statistics are deleted and then # include "head. H "using namespace STD; Class gt_cls {public: gt_cls (size_t val = 0): bound (VAL) {}// the constructor receives a bound value, there is a default real parameter 0 bool operator () (const STD: string & S) {// judge the input string object return S. size ()> = bound;} PRIVATE: STD: String: size_type bound;}; bool isshorter (const string & S1, const string & S2) {return S1. Size () <s2.size ();} static string make_plural (vector <string >:: size_type SZ, string word, string append) {return (SZ <= 1 )? Word: Word + append;} int main () {vector <string> words; string next_word; while (CIN> next_word) {words. push_back (next_word);} Sort (words. begin (), words. end (); // sort vector <string>: iterator end_unique = unique (words. begin (), words. end (); // unique sorts and returns the corresponding iterator words. erase (end_unique, words. end (); // Delete the last few duplicate stable_sort (words. begin (), words. end (), isshorter); // User-Defined Function intervention vector <string >:: size_type WC = count_if (words. begin (), words. end (), gt_cls (10); cout <WC <"<make_plural (WC," Word "," S ") <"10 characters or longer" <Endl; // The result STD: cout <count_if (words. begin (), words. end (), gt_cls (1)-count_if (words. begin (), words. end (), gt_cls (10) <"words gt1 but not gt10" <STD: Endl; return 0 ;}

14.36, which is too simple. It outputs the number of words with a length of 1 to 9 and more than 10.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.