Introduction to C + +-stl (Standard Template Library) Standard Template Gallery

Source: Internet
Author: User
Tags arithmetic
The STL contains three parts of the container Class (Container), the iteration sub (iterator), and the algorithm (algorithm). (1), Container
(2), AlgorithmIn a template the algorithm does not depend on the specific data type, and the generic algorithm goes further than the concrete container. For example, because the STL sort () function is completely generic, you can use it to manipulate almost any data set, including lists, containers, and arrays.
In the generic algorithm, the function object is used to introduce the difference of the same algorithm in different cases. It does not use inheritance and polymorphism, avoids the overhead of virtual functions, and makes the STL more efficient.
Generic algorithm classification:
A, lookup algorithm: There are 13 kinds of search algorithms to determine whether there is a specified value in the container. Equal_range (), Lower_bound (), and Upper_bound () provide a semi lookup form.
b, sequencing and General Order algorithm: A total of 14 kinds of sorting (sorting) and universal integer (ordering) algorithm, providing a variety of processing methods for ordering elements in a container.
The so-called whole order is classified according to certain rules, such as segmentation (partition) algorithm divides the container into two groups, one group consists of elements satisfying certain conditions, and the other is composed of elements that do not satisfy a certain condition.
3, deletion and substitution algorithm: There are 15 kinds of deletion and substitution algorithm.
4, permutation and combination algorithm: There are 2 kinds of algorithms.
The permutations and combinations are all arranged. For example: three characters {a,b,c} consists of 6 possible permutations: ABC,ACB,BAC,BCA,CAB,CBA; and the six permutations are arranged in the order above, the ABC is the smallest, the CBA is the largest, because the ABC is in full order (from small to large) and the CBA is in full reverse sequence (from big to small).
5, Generation and change algorithm: There are 6 kinds, including generation (generate), fill (fill) and so on.
6, relational algorithm: There are 7 relational algorithms, to compare two containers provide a variety of strategies, including equality (equal ()), Max (Max ()), Min (min ()) and so on.
7. Set algorithm: 4 sets (set) algorithms provide universal collection operations for any container type. Includes and (union), intersection (intersection), difference (difference) and symmetry difference (symmetric difference).
8, Heap algorithm: There are 4 kinds of heap algorithm. A heap is a form of a binary tree expressed as an array. The standard library provides a large Gan (MAX_HEAP), and its key words for each node are greater than the keywords of its child nodes.
9, arithmetic algorithm: This kind of algorithm has 4 kinds, uses the request including the header file <numeric>.
(3), an iterator (iterator) examines the elements within the container and traverses the element's data type.
Iterator Categories:
1. An input iterator (input iterator) can only step forward and not allow modifying elements referenced by that class iterator.
2. Output iterator, this class of iterators and input iterator very similar, can only step forward iterative elements, the difference is that the class iterator on the element only write power.
3. Forward iterator (forward iterator), a class of iterators that can read and write in a correct interval, with all of the attributes of the input iterator, some features of the output iterator, and the ability to step forward iteratively.
4. Bidirectional iterator (bidirectional iterator), a class of iterators that provide the ability to iterate over elements on a forward iterator basis.
5. Random Access iterator (random access iterator), which completes all of the above iterators, its own unique feature is the ability to perform arithmetic computations like pointers, rather than just stepping forward or backward iterations.
Container iterator:
Each of the container types defines its own iterator type, such as Vector:vector<int>::iterator iter;
Begin and end operations:
Each container defines a pair of functions named Begin and end, which is used to return the iterator. If there are elements in the container, the iterator returned by begin points to the first element:
Vector<int>::iterator iter = Ivec.begin ();
The above statement initializes ITER to the value returned by the vector operation named begin. Assuming the vector is not empty, after initialization, ITER means that the element is ivec[0].
Vectors and deque provided by Randomaccessiterator,list are provided by Bidirectionaliterator,set and map provided by Iterators is ForwardIterator
Iterator classes supported by the container
Vector Random access list bidirectional
Set bidirectional multiset bidirectional map bidirectional
Multimap bidirectional deque Random access stack does not support quequ does not support Priority_qu Eue does not support
Vector<int> Ivec (10,1);
For (Vector<int>::iterator Iter=ivec.begin (); Iter!=ivec.end (); ++iter)
{
cout << *iter <<endl; Use the * Access iterator to point to the element *iter = 0//assignment is 0
} eg:
#include <string>
#include <vector>
#include <set>
#include <deque>
Vector
Defines a set of
	Vector<double> V (0);
	Assign the set to
	int i = 0;
	for (i = 0; i<10;i++)
	{
		v.push_back (i);
	}
	Displays the elements in the collection for
	(i = 0;i < V.size (); i++) 
	{
		cout<<v[i]<< ",";
	}
	cout<<endl;
	Iterator traversal
	Vector<double>::iterator iterator;
	for (iterator = V.begin (); iterator!= v.end (); iterator++)
	{
		cout<< *iterator << ",";
	}
	cout<<endl;

A set collection (set) is a container in which the value of the element it contains is unique. And the elements in the collection are arranged in a certain order.
Defines a set
	set<string> s;
	S.insert ("Apple");
	S.insert ("Orange");
	S.insert ("banana");
	Set<string>::iterator iter;
	for (iter = S.begin (); Iter!= s.end (); iter++)
	{
		cout<<*iter<< ",";
	}
	cout<<endl;
Output: Apple,banana,orange,
Deque
Defines a bidirectional queue
	deque<string> DEQ;
	Deq.push_back ("hell");//tail insert
	deq.push_back ("fill");
	Deq.push_front ("Dell");//head insert
	deque<string>::iterator pos;
	pos = Deq.begin ();
	POS + +;
	Deq.insert (POS, "Kill");  Position Insert
	Cout<<deq.front () <<endl;
	Cout<<deq.back () <<endl;

Related Article

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.