Basic concepts of generic programming
- Writing programs that do not depend on specific data types
- Abstract the algorithm from a specific data structure to become a universal
- C + + templates provide a key foundation for generic programming
Terminology: Concepts
- Used to define the type of data with a certain function. For example:
- The concept of "can be compared to all data types (with comparison operators) of size" is recorded as comparable
- The concept of "having a public copy constructor and a data type that can be assigned with ' = ' is recorded as assignable
- The concept of "can be larger than size, have a public copy constructor and can be assigned with ' = ' All data types" is sortable
- For two different concepts A and B, if all the functionality required by concept A is also the function required by concept B, then concept B is a sub-concept of concept A. For example:
- Sortable is not only the comparable concept, but also the sub-concept of assignable.
Terminology: Models
- Model: A data type that conforms to a concept is called a model of the concept, for example:
- The int type is the model of the comparable concept.
- Static array type is not a model of the assignable concept (unable to assign a value to the entire static array with "=")
Use concept to make template parameter name
- Many STL implementation codes use concepts to name template parameters.
- Give the concept a name and use that name as the template parameter name.
For example
Represents a prototype of a function template such as Insertionsort:
Template <class sortable>void insertionsort (sortable a[], int n);
STL Introduction
The standard Template Library, or STL, provides some very common data structures and algorithms
STL Introduction
- The standard Template Library, or STL, defines a set of conceptual systems that provide a logical basis for generic programming
- The parameters of each class template and function template in STL are defined by the concepts in this system.
- When using STL templates, type parameters can be either existing types in the C + + standard library or custom types-as long as these types are models of the required concepts.
Basic components of STL
- Container (Container)
- Iterators (iterator)
- Function object
- Algorithm (algorithms)
The relationship between the basic components of STL
- Iterators (iterators) are the bridge between algorithms and containers.
- The iterator is used as the parameter of the algorithm, through iterators to access the container instead of directly as the parameter of the algorithm.
- Use the function object as a parameter of the algorithm rather than as part of the algorithm for the operation performed by the function.
- Using the STL-provided or custom iterators and function objects, together with the STL algorithm, you can combine a variety of functions.
Basic components of STL--containers (container)
- An object that holds and contains a set of elements.
- Basic Container class Templates
- Sequential container
- Array (arrays), vector (vector), deque (double-ended queue), Forward_list (single-linked list), list
- (ordered) Associative containers
- Set (collection), Multiset (multiset), map (map), Multimap (Multimap)
- Unordered associative container
- UnorderedSet (unordered collection), unorderedmultiset (unordered multiset)
- UnorderedMap (unordered map), Unordermultimap (unordered multimap)
- Container Adapter
- Stack (stack), queue (queued), Priority_queue (priority queue)
- To use a container, you need to include the corresponding header file
Basic components of STL--iterators (iterator)
- Iterators are generalized pointers that provide a way to sequentially access each element in a container
- Provides a way to sequentially access each element in the container;
- You can use the "+ +" operator to get an iterator that points to the next element;
- You can use the "*" operator to access the element that an iterator points to, and if the element type is a class or struct, you can also access a member of the element directly by using the "-a" operator;
- Some iterators also support the use of the "--" operator to obtain an iterator to the previous element;
- Iterators are pointers to generalization: pointers have the same characteristics, so pointers themselves are iterators;
- Using an iterator independent of the STL container, you need to include the header file.
Basic components of STL-function objects (functions object)
- An object that behaves like a function, which can be called as if it were called a function.
- function objects are generalized functions: any ordinary function and any object that overloads the class of the "()" operator can be used as a function object
- Using STL's function object, you need to include the header file
STL's basic component-algorithm (algorithms)
- STL includes more than 70 algorithms
- For example: sorting algorithm, elimination algorithm, counting algorithm, comparison algorithm, transform algorithm, permutation algorithm and container management etc.
- Can be used extensively with different objects and built-in data types.
- Using the STL algorithm, you need to include the header file.
- Example 10-1 reads a few integers from the standard input, stores them in a vector container, and outputs their opposite number.
STL Program Examples
An implementation of the transform algorithm:
Template <class Inputiterator, class Outputiterator, Class Unaryfunction>outputiterator transform (inputiterator First, Inputiterator last, outputiterator result, unaryfunction op) {for (; first! = last; ++first, ++result) *res Ult = OP (*first); return result;}
- The transform algorithm sequentially traverses the elements pointed to by first and last two iterators;
- The value of each element as an argument to the op of the function object;
- Outputs the return value of the OP through the iterator result sequence;
- After the traversal is complete, the result iterator points to the next position of the last element of the output, and transform returns the iterator
This text is referenced from C + + language Programming (4th Edition), PhD, Tsinghua University Press
C + + generic programming and STL Template Library (1)---Introduction of generic program design and STL introduction and structure