1#include <iostream>2#include <algorithm>3#include <bitset>4#include <deque>5#include <vector>6 7 //Usage 18 usingstd::cout;9 usingstd::cin;Ten One //Usage 2 (most commonly used) A using namespacestd; - - voidtest1 () the { -cout <<'?'<< Std::endl;//Std::endl Usage 3 (Direct namespace::(domain) identification) -cout <<'@'<<'\ n'; - } + - //function template Demonstration 1 + //template<typename t=int>//only allow default template parameters to be used above the class template ATemplate<typename t> at /*int*/t Max (t a,t b) - { - returnA>b?a:b; - } - - //function Template Demonstration 2 inTemplate<typename t> - BOOLEquivalent (Constt& A,Constt&b) { to return! (A < b) &&! (b <a); + } - the //class Template *Template<typename t=int>//default parameters, which allow only the default template parameters to be used above the class template $ classbignumber{Panax Notoginseng T _v; - Public: the BigNumber (T a): _v (a) {} +InlineBOOL operator< (Constbignumber& b)Const;//equivalent to (const bignumber<t> B) A }; the + //implementing member functions outside of a class template -Template<typename t> $ BOOLBignumber<t>::operator< (Constbignumber& b)Const $ { - return_v <B._v; - } the - //regular functions can handle only one data type at a timeWuyi voidSwapint&a,int&b) { the inttemp =A; -A =b; Wub =temp; - } About $ //Use the template keyword to declare a type named T (soft type) -Template<typename t>voidSwap (t& t1, t& T2);//Statement -Template<typename t>voidSwap (t& t1, t& T2) {//definition (Implementation) -T tmpt;//declares the T-type variable tmpt; Atmpt = T1;//assigns the T1 value to Tmpt +T1 =T2; thet2 =tmpt; - } $ the voidtest2 () the { thevector<int>VI; the intA; - while(true) in { thecout<<"Enter an integer and press 0 to stop the input:"; theCin>>A; About if(a==0) Break; the Vi.push_back (a); thevector<int>:: iterator iter; the for(Iter=vi.begin (); Iter!=vi.end (); + +iter) +cout<<*ITER; - } the }Bayi the intMainvoid) the { - test1 (); - test2 (); the the //template functions automatically recognize data types, so you don't have to rewrite several function codes, and a template can implement the intx=1982, y=1983; thecout<<"Max is"<<::max (x, y) <<Std::endl; - floatf1=3.1416f, f2=3.15F; thecout<<"Max is"<</*(float)*/:: Max (F1,F2) <<Std::endl; the Doubled1=6.18e10; the DoubleD2=6.18e11;94cout<<"Max is"<</*(Double)*/:: Max (D1,D2) <<Std::endl; the theBignumber<> A (1), B (1);//with default parameters, "<>" cannot be omitted theStd::cout << equivalent (A, b) <<'\ n';//automatic derivation of function template parameters98Std::cout << equivalent<Double> (1,2) <<'\ n'; About - //Template Method101 intNUM1 =199, num2 =276;102cout <<"NumA:"<< NUM1 <<"\tnumb:"<< num2 <<Endl;103::swap<int>(NUM1, num2); 104cout <<"Swap:numa:"<< NUM1 <<"\tnumb:"<< num2 <<Endl; the 106 floatNumA =3.1416f, NumB =6.18f;107cout <<"\t\tnuma:"<< NumA <<"\tnumb:"<< NumB <<Endl;108::swap<float>(NumA, NumB); 109cout <<"\t\tswap:numa:"<< NumA <<"\tnumb:"<< NumB <<Endl; the 111Std::cin.Get();//GetChar (); cin>>x; the return 0;113}
Use of C + + templates