Touching C++ Standard Template Library…

The Standard Template Library (STL) of C++14 had huge influence on C++ Standard Library. The STL provides containers that can be used with any built-in type and with any user-defined type that supports some elementary operations. STL algorithms are independent of containers, which significantly reduces the complexity of the library. Algorithms works on any container that supports iterators.

The STL achieves this through templates. It provides compile-time polymorphism that is often more efficient than traditional run-time polymorphism. Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>

// Define function template Add
template<class T>
T Add(T t1, T t2) { return t1 + t2; }

void printVector(const auto&);

int main()
{
	// Using the function template on integers.
	int i{ Add(3, 4) };
	std::cout << "i = " << i << std::endl;

	// Useing the function template on doubles.
	double d{ Add(1.1, 2.2) };
	std::cout << "d = " << d << std::endl;

	// Using the function template on string.
	std::string s{ Add(std::string{ "Hello " }, std::string{ "World" }) };
	std::cout << "s = " << s << std::endl;

	//Using vector containers(example for class templates) and some of their functions.
	std::vector<int> integers{ 3, 7, 11, 13 };
	std::cout << "integers: " << std::endl;
	printVector(integers);
	std::vector<double> doubles{ 1.1, 5.4, 77.2, -1.2 };
	std::cout << "doubles: " << std::endl;
	printVector(doubles);
	integers.push_back(-3);
	integers.push_back(22);
	integers.erase(begin(integers) + 1); // erases 7
	integers.insert(begin(integers) + 2, 1); // inserts 1 after 11
	int ilength = integers.size();
	std::cout << "size of integers: " << ilength << std::endl;

	// Useful functions from STL, from header files numeric and algorithm.
	int total = accumulate(begin(integers), end(integers), 0);
	std::cout << "total of integers: " << total << std::endl;
	auto thirteen = find(begin(integers), end(integers), 13);
	*thirteen = -13;
	std::cout << "integers(before sorting): " << std::endl;
	printVector(integers);
	sort(begin(integers), end(integers));
	std::cout << "integers(after sorting): " << std::endl;
	printVector(integers);

	// std::iota will fill an iterator range with successively incremented values.
	std::vector<int> consecutive(10);
	std::iota(begin(consecutive), end(consecutive), 0);
	std::cout << "vector created using 'iota': " << std::endl;
	printVector(consecutive);
}

void printVector(const auto& v)
{
	for (const auto& element: v) 
	{
		std::cout << element << std::endl;
	}
	std::cout << std::endl;
}

Output:

Templates