This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13940|Reply: 0

[C++] Thinking about templates in C++

[Copy link]
Posted on 10/4/2014 8:45:19 PM | | |
In high-level languages, we use overloading for functions, which is used in functions with different functions but with the same number of parameters, and templates are needed for different types and the same number of parameters
C++ applet

  1. #include<iostream>
  2. using namespace std;

  3. template <class T>
  4. T max(T a,T b)
  5. {
  6. return         a>b?a:b;
  7. }

  8. int main()
  9. {
  10. int a,b;cin>>a>>b;
  11. cout<<max(a,b);
  12. return 0;
  13. }
Copy code

This is a simple example: the keyword template<class T> is the beginning of a template structure, so that we don't need to write several C++ code with the same function repeatedly when looking for the maximum value
Note that when calling a function, its type is automatically matched. It doesn't need to be shown.
The above is the most basic usage, but we will encounter the following situations

  1. #include<iostream>
  2. using namespace std;

  3. template <class T,class E>
  4. E max(T a,E b)
  5. {
  6. return         a>b?a:b;
  7. }

  8. int main()
  9. {
  10. int a;float b;cin>>a>>b;
  11. cout<<max(a,b);
  12. return 0;
  13. }
Copy code

What to do when our function needs two different types, I think you can understand it after reading the above code. But anyway, what they have in common is that the types are automatically matched.
In fact, we also have mistakes, if we can't match the type well, that is, "the bull's head is not right", then the compiler's automatic matching will change the parameter type according to the rules of implicit type conversion, which will eventually lead to the loss of accuracy of the result.
Look at the code, you can try.
  1. #include<iostream>
  2. using namespace std;

  3. template <class T,class E>
  4. E max(T a,E b)
  5. {
  6. return         a>b?a:b;
  7. }

  8. int main()
  9. {
  10. int a;float b;cin>>a>>b;
  11. cout<<max(a,b);
  12. return 0;
  13. }
Copy code
The above content is not very difficult, but there are also many places that can be explored, I don't know what your opinions are





Previous:c Mini program written by Inline
Next:I posted a logo made for my forum and showed it to everyone!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com