Templates make it possible to write small algorithms that work with containers of different value types.

Generic Algorithms

generic_algorithms.cpp
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <vector>

template <typename T>
T sumValues(const std::vector<T>& values) {
    T total{};

    for (T value : values) {
        total += value;
    }

    return total;
}

int main() {
    int bonus = ;
    int extra = ;

    std::vector<int> scores{3, bonus, extra};
    int total = sumValues(scores);

    std::cout << "count=" << scores.size() << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
generic algorithm A generic algorithm focuses on the operations it needs, such as adding values, instead of one concrete type.