Six major STL components-imitation function, six major stl components

Source: Internet
Author: User

Six major STL components-imitation function, six major stl components

Functor is to make the use of a class or class template look like a function. The implementation is to overload operator () in a class or class template. This class or class template has the behavior of similar functions. The Imitation function is a smart function, just like the behavior of a smart pointer, which can be seen as a pointer. However, smart pointers are defined as class objects, so they have other capabilities while having the pointer function. The capabilities of function simulation can also surpass operator (). Because the imitation function can have member functions and member variables, this means that the imitation function has the state. Another benefit is that they can be initialized during execution.

Predefined function-like arithmetic class

Addition: plus <T>; subtraction: minus <T>; multiplication: multiplies <T>; Division: divides <T>; remainder: modulus <T>; negative: negate <T>

Relational computing

Equal to: effec_to <T>; not equal to: not_effec_to <T>; greater than: greater <T>; greater than or equal to: greater_equal <T>; less than: less <T>; less than or equal: less_equal <T>

Logical operations

Except for the negative value of one dollar, the others are binary imitation functions. And: logical_and <T>; or: logical_or <T>; NO: logical_not <T>

The following is an example of a user-defined coding method. A user needs to calculate the weights of all characters in a file to create the final encoding process for each character, one step is to traverse the existing character weight table to find whether the characters obtained from the file already exist in the table, if a node exists, add the weight of the character in the table. If the character does not exist, create a new node to store the character and add the node structure to the existing weight table. Considering that you need to search for characters in the weight table and create a Huffman Tree later, you may need to sort the items in the table. Therefore, the vector <Node> is used as the storage container, you can use the sort and find algorithms in the algorithm header file for searching and sorting.

1 # include <iostream> 2 # include <fstream> 3 # include <string> 4 # include <vector> 5 # include <algorithm> 6 # include <functional> // for bind2nd func; 7 8 # ifndef HUFFMAN_H 9 # define HUFFMAN_H10 11 using namespace std; 12 13 class Huffman14 {15 public: 16 Huffman (void); 17 // huffman (File file, File srcTable ); 18 void encode (const string & file); 19 void decode (const string & file, const string & srcTable) co Nst; 20 ~ Huffman (); 21 private: 22 // table node23 typedef struct24 {25 char letter; 26 int level; 27 int parent; 28 int direction; //-1 for no parent, 0 for left, 1 right; 29} Node; 30 vector <Node> nodeTable; // can be sorted; 31 32 // this function is used for find_if to detect whether a character already exists; 33 // template <typename T1, typename T2> 34 class Comparer: public binary_function <Node, char, bool> 35 {36 public: 37 Comparer (const char ch ): _ ch (ch) {}; 38 39 const bool operator () (const vector <Node >:: value_type & node, char ch) const40 {41 return node. letter = ch; 42} 43 44 private: 45 char _ ch; 46}; 47}; 48 49 # endif
1 # include "Huffman. h "2 3 Huffman: Huffman (void) 4 {5 // dummies; 6} 7 8 void Huffman: encode (const string & file) 9 {10 ifstream fin; 11 fin. open (file. c_str (); 12 char ch; 13 while (fin. get (ch) 14 {15 if (nodeTable. size ()! = 0) 16 {17 // simulation function 18 vector <Node>: iterator result = find_if (nodeTable. begin (), nodeTable. end (), bind2nd (Comparer (ch), ch); 19 20 if (result = nodeTable. end () 21 {22 Node * node = new Node; 23 node-> letter = ch; 24 node-> level = 1; 25 node-> parent = 0; 26 node-> direction =-1; 27} 28 else29 {30 result-> level + = 1; 31} 32} 33 else34 {35 Node * node = new Node; 36 node-> letter = ch; 37 node-> level = 1; 38 node-> p Arent = 0; 39 node-> direction =-1; 40} 41} 42 fin. close (); 43 44 45 // huffman tree; 46 47} 48 49 void Huffman: decode (const string & file, const string & srcTable) const50 {51 // dummies; 52} 53 54 Huffman ::~ Huffman (void) 55 {56 // dummies; 57}

Here, the function call process is like this:Find_if (nodeTable. begin (), nodeTable. end (), Comparer (ch ))Comparer (ch) is only an anonymous method of the Comparer class. The real call of the overloaded operator () is the find_if template function that will be viewed in the next step.Pred (* first)In the code, if you do not use find and use find_if because you need to search for a qualified type written by yourself instead of prime type. For the find_if function prototype, refer to the following, from the prototype, we can see that the first parameter is implemented by default:

1 template<class InputIterator, class Predicate>2 InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )3 {4     for ( ; first!=last ; first++ ) if ( pred(*first) ) break;5     return first;6 }

The bind2nd function is used to convert binary functor (bf) to unary functor (uf) and has a similar bind1st, which must contain FunctionalHeader file; the compared functions must be inherited.Binary_functor <typename T1, typename T2, typename T3>T3 is generally a bool value. The binary_functor prototype is as follows:

1 template<class Arg1,class Arg2,class Result>2 struct binary_function 3 {4       typedef Arg1 first_argument_type;5       typedef Arg2 second_argument_type;6       typedef Result result_type;7 };

Here we use bind2nd to directly bind the obtained char parameter to the Comparer imitation function. Of course, we can directly pass the parameter to the human method in the Comparer imitation function, from Huffman. h and Huffman. the comments of cpp show that there are different feasible methods.

 

Related Article

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.