When is the best place to use a C ++ template?

I'm learning C ++ right now, and now I know the basic concept of a template that acts as a generic type, and I found almost all the templates used in C ++, So what I really want to know is when should we use a template? Can someone complete their experience for me about the C ++ template? When will you consider using a template?

Addendum: if we defined such a function

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

but we want to pass an object (a self-defined class) for comparison, how can we implement it?

Supplement2: in the answer below, someone wrote this sample code

template <class myType>
const myType& GetMax (const myType& a, const myType& b) {
    return (a<b?b:a);
}

template <class myType, class Compare>
const myType& GetMax (const myType& a, const myType& b, Compare compare) {
    return (compare(a,b)?b:a);
}

It is right? can we just pass the name of the function as a parameter to the myType class?

+3
source share
10

Re: . , :

template <class myType>
const myType& GetMax (const myType& a, const myType& b) {
    return (a<b?b:a);
}

template <class myType, class Compare>
const myType& GetMax (const myType& a, const myType& b, Compare compare) {
    return (compare(a,b)?b:a);
}

Samle: C-:

bool c_strings_less(const char* a, const char* b)
{
    return std::strcmp(a, b) < 0; //is a less than b
}

const char* greater = GetMax("hello", "world", c_strings_less);

std:: max. ( , , ++ , "".)

, , GetMax , operator > .

+1

G'day,

, , , .

, int , float MyClass.

, .

, Animal, -, makeSound(). , , makeSound . , , , , - .

-, , Lion, - makeSound, - .

: , " C++" ( Amazon link), .

,

+8

, , , , .

, ( , , )

//----- the container
template <class T>
class Stack
{
public:
    T* stackPtr;
}

//----- example
void main()
{
    typedef Stack<float> FloatStack;
    typedef Stack<int> IntStack;
}

float int .

+3

: : . , ( ...), , .

STL/boost .

+2

, operator > , .

, :

class fraction
{
private:
    int _num, _den;

public:
    fraction(int num, int den)
    {
        if (den >= 0)
        {
            _num = num;
            _den = den;
        }
        else
        {
            _num = -num;
            _den = -den;
        }
    }

    fraction(const fraction &f)
    {
        _num = f._num;
        _den = f._den;
    }

    bool operator > (const fraction &f) const
    {
        return (_num * f._den) > (f._num * _den);
    }

    bool operator == (const fraction &f) const
    {
        return (_num * f._den) == (f._num * _den);
    }
};

.

int main(int argc, char* argv[])
{
    fraction a(1,2); // 0.5
    fraction b(3,4); // 0.75
    assert(GetMax/*<fraction>*/(a,b) == a);
    return 0;
}
+2

, .

, ,

class MyThingManager
{
  void add( MyThing& mything ); 
  //...
};

... , , , . , copy/paste/replace - , , :

template< class ThingType >
 class ThingManager
    {
      void add( ThingType& thing ); 
      //...
    };

, .

, , - , :

template< class MyType >
void addPi( MyType& value )
{
    value += PI;
}

, ( ) , .

" " .

, , . , , . "++ Template Meta-Programming" "Modern ++ Design" , .

+1

( ( ) , ?)

, p > . .

, , .

/Tobias

+1

, ...

2 int, int 2 ,

.

0

KNOWN AT COMPILE TIME. , (std::vector ), :

template <int N, typename T > class MyType

, , MyType < 2, int > MyType < 3, int > .

, : . .

http://ubiety.uwaterloo.ca/~tveldhui/papers/priority.html .

0

, . , , , , . - , .

On any common development platform, the standard library provides a high-quality implementation of many traditional old-school patterns. Using standard classes and library functions does not require writing new templates. For example, it provides std :: max (), which matches your example.

0
source

Source: https://habr.com/ru/post/1716496/


All Articles