1/18
Flashcards about Templates, Function Overloading, and Standard Template Library (STL) in C++
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
DRY (Don't Repeat Yourself) Code
A coding principle to avoid duplicating code; templates are used to achieve this.
Templates
Templates are used as placeholders for data types, allowing functions and classes to work with different types without being rewritten.
When can we use templates?
All functions have the same number of parameters, the same code block, and the same type pattern.
How can you define a template?
Using template
How do templates run?
The compiler interprets the parameters passed in to determine the specific type.
How can you set the template type manually?
By specifying the type explicitly when calling the template function.
When would you use templates over function overloading?
When you want the same function to work with different types while maintaining the same logic.
Examples of Algorithms in STL
Sorting, searching, partitioning, and standard array algorithms.
Examples of Containers in STL
Vector, list, array, queue, stack, set, and map.
What is STL?
Standard Template Library, a set of C++ template classes for common programming data structures and functions.
What does sort(arr, arr+size) do?
Sort an array from the beginning to the end.
What does reverse(arr, arr+n) do?
Revert elements from the beginning to the end of the array.
What does count(arr, arr+size, 0) do?
Count the number of 0s from the beginning to the end of the array.
What does copy_n do?
Copy all elements from one array to another array.
What parameters does copy_n take?
Source array, size of array, and target array.
Function Overloading
Different functions with the same name but different implementations.
What are Functions in the context of STL?
It allows you to overload function operators.
What are Iterators in the context of STL?
They allow you to iterate over a sequence of values.
Using references (&) in templates
This will modify the original variables.