1 Basic concepts of generic programming
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 "=")
2STL 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: containers, iterators, function objects, algorithms
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.
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
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.
//10-1stl A program instance, reads several integers from the standard input, stores them in a vector container, and outputs their opposite number#include <iostream>#include<vector>#include<iterator>#include<algorithm>#include<functional>using namespacestd;intMain () {Const intN =5; Vector<int> S (N);//Container for(inti =0; i < N; i++) Cin>>S[i]; Transform (S.begin (), S.end (), Ostream_iterator<int> (cout," "), negate<int>()); cout<<Endl; return 0;}
//implementation of the transform algorithm, no more knocking in the machinetemplate<classInputiterator,classOutputiterator,classUnaryfunction>Outputiterator Transform (inputiterator First, inputiterator last, outputiterator result, unaryfunction op) { for(; First! = last; ++first, + +)result)*result = OP (*First ); returnresult;}//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
Part10 generic Programming and C + + Standard Template Library 10.1 generic programming and STL structure