C ++ standard template library Review

Source: Internet
Author: User

I. Generic programming
Concepts and terms related to the standard template library
Containers in the C ++ standard template library
Iterator
Algorithms in the Standard C ++ Library
Function object
Generic programming
Write programs as common as possible
Abstracts algorithms from specific data structures to become common
The C ++ template lays a key foundation for generic program design.
Ii. STL is an example of generic programming
Container)
Iterator)
Algorithm (algorithms)
Function object)
Iii. namespace)
A namespace aggregates different identifiers in a namescope.
To resolve naming conflicts
For example, declare a namespace NS:
Namspace NS {
Class File;
Void Fun ();
}
The method for referencing an identifier is as follows,
NS: File obj;
NS: Fun ();
The identifiers that do not declare a namespace are all in the unnamed namespace.

You can use using to specify the namespace.
For example, after the following declaration: using NS: File; File can be directly referenced in the current scope.
Using namespace std; All identifiers in The namespace std can be directly referenced
In the new C ++ standard library, all identifiers are declared in the namespace std, and header files do not use extensions.
Iv. Container
A container class is an object that contains a set of elements or element sets.
Heterogeneous containers and similar containers
Ordered container and associated container
Seven basic containers:
Vector, deque, list, set, multiset, map, and multimap)

5. Container Interfaces
Common container operators
= ,! =,>,> =, <, <=, =
Method (function)
Iteration Method
Begin (), end (), rbegin (), rend ()
Access Method
Size (), max_size (), swap (), empty ()

VII. Adapter
An adapter is an interface class.
Provides New interfaces for existing classes.
The purpose is to simplify, constrain, secure, hide, or change the set of services provided by the modified class.
Three types of adapters:
Container Adapter
It is used to expand seven basic containers, which are combined with the sequential container to form a stack, queue, and priority queue container.
Iterator Adapter
Function Object Adapter.
Iterator
Iterators are object-oriented version pointers that provide methods to access each element in the container and sequence.
8. Algorithms
The C ++ standard template library contains more than 70 algorithms.
These include search algorithms, sorting algorithms, elimination algorithms, counting algorithms, comparison algorithms, Transformation algorithms, replacement algorithms, and container management.
One of the most important features of these algorithms is their uniformity, which can be widely used for different objects and built-in data types.
Sequential container
Sequential container Interface
Insert Method
Push_front (), push_back (), insert (), operator "="
Delete Method
Pop (), erase (), clear ()
Iterative access method
Use iterator
Other sequential container access methods (without modifying the access methods)
Front (), back (), subscript [] Operator
Ordered container -- Vector
A vector is a sequential container used to hold an indefinite linear sequence (that is, a linear group) and provides fast Random Access to the sequence (also called direct access)
A vector is a dynamic structure. Its size is not fixed and can be increased or decreased when the program is running.
Range 2 ~ The prime number in N, and N is input by the keyboard when the program is running.
//. Cpp
# Include <iostream>
# Include <iomanip>
# Include <vector> // contains the header file of the vector container.
Using namespace std;
Int main ()
{
Vector <int> A (10 );
Int n;
Int primecount = 0, I, j;
Cout <"Enter a value> = 2 as upper limit :";
Cin> n;
A [primecount ++] = 2;

For (I = 3; I <n; I ++)
{If (primecount = A. size ())
A. resize (primecount + 10 );
If (I % 2 = 0)
Continue;
J = 3;
While (j <= I/2 & I % j! = 0)
J + = 2;
If (j> I/2) A [primecount ++] = I;
}
For (I = 0; I <primecount; I ++) // output Prime Number
{Cout <setw (5) <A [I];
If (I + 1) % 10 = 0) // wrap each output 10 times
Cout <endl;
}
Cout <endl;
}
Ordered container-double-end queue
A dual-end queue is a queue that removes access permissions. Elements can be entered and exited from both ends of the queue, or directly accessed through the subscript operator.
Use a dual-end queue container to save the double-precision numerical sequence
Ordered container -- list
Lists are mainly used to store two-way linked lists and can be traversed from any end. The List also provides the splicing operation to insert elements from a sequence into another sequence.
Input 10 integers from the keyboard and use these integers as the node data to generate a linked list and output the node values in the chain table in sequence. Enter an integer to be searched on the keyboard and search for the integer in the linked list. If the integer is found, delete the node where the integer is located (if multiple times are found, delete all nodes ), output the linked list after the node is deleted. Clear the linked list before the program ends.
//. Cpp
# Include <iostream>
# Include <list>
Using namespace std;

Int main ()
{
List <int> Link; // you can create a list to store an integer linked list.
Int I, key, item;
For (I = 0; I <10; I ++) // input 10 Integers to insert them to the header in sequence.
{
Cin> item;
Link. push_front (item );
}
Cout <"List:"; // output linked List

List <int>: iterator p = Link. begin ();
While (p! = Link. end () // output data of each node until the end of the linked list
{Cout <* p <"";
P ++; // point P to the next node.
}
Cout <endl;
Cout <"enter an integer to be deleted :";
Cin> key;
Link. remove (key );
Cout <"List:"; // output linked List
P = Link. begin (); // causes P to point to the header again.
While (p! = Link. end ())
{Cout <* p <"";
P ++; // point P to the next node.
}
Cout <endl;
}
9. Container Adapter
Container adapters are used to expand seven basic containers.
Stack container
Use the adapter in combination with a basic container
The deque ordered container in the application standard Library generates an integer stack.
Queue container
The first-in-first-out data structure is achieved by combining the adapter with a basic container.
The deque ordered container in the application standard Library generates an integer standard queue.
What is an iterator?
The iterator is the pointer of the object-oriented version.
The pointer can point to an address in the memory.
The iterator can point to a location in the container.
Each container class template in STL defines a group of corresponding iterator classes. Using the iterator, Algorithm functions can access the elements at the specified position in the container without worrying about the specific types of elements.
Iterator type
Input iterator
It can be used to read data from a sequence.
Output iterator
Allow writing data to a sequence
Forward iterator
It is both an input iterator and an output iterator, and can traverse the sequence in one way.
Bidirectional iterator
Similar to the forward iterator, but data can be traversed in both directions.
Random Access iterator
It is also a bidirectional iterator, but can jump between any two positions in the sequence.
Iterator Adapter
The iterator adapter is a class used to extend (or adjust) The iterator function. It is also called an iterator, but this iterator is obtained by changing another iterator.
Reverse iterator
By redefining the incremental and descending operations, the behavior is inverted.
Insert iterator
Convert the value assignment operation to the insert operation. With this iterator, the algorithm can execute the insert behavior instead of overwriting the behavior.
Apply the reverse iterator and the post-insert iterator to operate the elements in the vector container.
Auxiliary functions related to the iterator
Advance () function
Increase the iterator position. The increase is determined by the parameter.
Distance () function
Returns the distance between iterators.
Function iter_swap ()
Exchange the element values pointed to by the two iterators
Use three iterator auxiliary functions to operate the elements in the list container.
Algorithms in the Standard C ++ Library
The algorithm itself is a function template.
Non-mutating algorithms)
Do not directly modify the algorithm of the operated container content
Mutating algorithms)
You can modify the elements of the containers they operate on.
Sorting algorithms
Numerical Algorithm
Example of Algorithm Application
Use an immutable Sequence Algorithm to analyze data sequences
Use a Variable Sequence Algorithm to copy, generate, delete, replace, reverse, and rotate data sequences.
Use sorting algorithms to perform operations on Sequences
Use numeric algorithms to operate data sequences
Function object
An object with a behavior similar to a function can have no parameters or several parameters. Its function is to obtain a value or change the operation status.
All common functions and any objects that overload the class that calls operator () satisfy the features of function objects.
Some standard function objects are also defined in STL. functional objects can be divided into arithmetic operations, relational operations, and logical operations. To call these standard function objects, you must include the header file <functional>.
Summary and review suggestions
Main Content
Generic programming, concepts and terms related to the standard template library, containers in the C ++ standard template library, iterators, algorithms in the Standard C ++ library, and function objects
Goals achieved
First understand the concept of generic programming, and learn how to use the C ++ standard template library (STL ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.