c++c++11

(C++) What does it mean when a data type is between a '<' and '>' sign?


I'm reading Bjarne Stroustrup's book on C++ and he uses things like vector<int> or complex<double>. What does it mean when a data type like int is between a < and > sign?

Tried Googling but it won't recognize my < or > enter image description here

enter image description here


Solution

  • They're templates.

    Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

    for example:

    template <class C>
    C add (C a, C b) {
      C result = a + b;
      return (result);
    }
    
    int a = 1;
    int b = 2;
    
    add<int>(a, b); //returns 3
    
    float c = 1.5
    float d = 0.5
    
    add<float>(c, d) // returns 2.0