C + + generic functions and template classes

Source: Internet
Author: User

What is generic programming?

In simple terms, generic programming means a wide variety of programming. Specific types can have different implementations, but for wide-type programming, you can specify parameter types or call types when you need to invoke them.

Generic programming is a programming method that is based on the most abstract representation of discovering efficient algorithms. In other words, the algorithm is the starting point and the most common set of necessary conditions that can make it work and work efficiently.

As you can imagine, many algorithms require the same set of prerequisites, and these set of prerequisites are implemented differently. The STL Standard Template Library is an example of generic programming.

How does a generic function write?

#include <iostream>using namespace Std;template<typename t>t * My_find (T *array,t n,t x) {t* p = array;int i;for (i=0;i<n;i++) {if (*p = = x) {return p;} p++;} return 0;} int main () {int a[] = {1,2,3,4,5,6,7,8,10};int *result = my_find<int> (a,10,3);cout<< (*result) << "\ n" <<endl;return 0;}

The above example is an implementation of a generic function.

The meaning of this phrase is to set a generic type T. The next step is to use T as a parameter type directly.

int *result = my_find<int> (a,10,3);

This is the calling method of the generic function. Angle brackets Specify the specific type of the function generic, and then pass in the parameter.

How to write the template class?

In addition to generic functions, the most important thing is that the generic class is also the template class.

#include <iostream>using namespace Std;template<class t>class operate{public:static t Add (t a,t b) {return a + b;} Static T Mul (T a,t b) {return a*b;} Static T Judge (T a,t b=1) {if (a>=0) return a;elsereturn A/b;}}; int main () {int A,b,c,d,e,x,y,z;a=1,b=2,c=3,d=4,e=5;x=operate<int>::add (a, b); Y=operate<int>::mul (c,d) ; Z=operate<int>::judge (e,b);cout<<x<< "" <<y<< "" <<z<< "" <<endl; return 0;}

The above example is an implementation of a template class.

Template<class t>

This statement defines a template class type.

X=operate<int>::add (A, b); Y=operate<int>::mul (c,d); Z=operate<int>::judge (e,b);

When you invoke a method that uses a template class, you also specify the concrete implementation class of the template class with angle brackets.

The above is the basic use of C + + generic functions and template classes.

Generic programming is widely used in STL Standard Template Library, in-depth learning of STL, of course, to master the generic programming.

C + + generic functions and template classes

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.