STL-- Overview

  1. One of the core strengths of the C ++ language isFacilitate software reuse
  2. C ++ has two aspects reflect reuse:
  • Object-oriented thinking: inheritance and polymorphism, standard library
  • Generic programming ideas: template class system, as well as the Standard Template Library STL

A. Generic programming

  • Simply put, it is to use a template programming method.
  • Some commondata structure(Such as linked lists, arrays, binary), andalgorithm(Such as sort, search) written template, thereafter, regardless of the data structure put what objects, the algorithm for what kind of object, you do not have to re-implement data structures, algorithms rewritten.
  • Standard library functions(Standard Template Library) is some commonThe data collection template structures and algorithms
  • With STL, without having to write standard data structures and algorithms most, and achieved very high performance.

II. The basic concepts of the STL

  1. container: Can accommodate a variety of generic data types of data structures, it isClass Template
  2. Iterator: Element can be used to access a container, similar topointer
  3. algorithm: To the operating elements of the containerFunction template
    sort()函数来对一个vector中的数据进行排序
    find()来搜索一个list中的对象
  4. Regardless of the type of algorithm itself and their operation data, so they can be used on any simple array data structure from the container to the highly complex.
  5. E.g:
int array[100];
该数组就是容器,而int * 类型的指针变量就可以作为迭代器,sort算法可以作用于该容器上,对其进行排序;
sort(array, array+70);  //将前70个元素排序,其中array 和array+70 就是迭代器 

III. Overview of container

  1. Data structures may be used for data (basic types of variables, objects, etc.) to store various types of class template are divided into three kinds:
  2. Sequential container: vector,deque,list
  3. Associative containers:set,multiset,map,multimap
  4. Container adapter:stack,queue,priority_queue
  5. When an object is inserted into the container, the inserted object is areplica. Many algorithms, such as sort, search, the elements required for the algorithm are compared, and some sort of container itself is, therefore, an object class belongs into the container, and often also should override == <operators.

(A). Introduction sequential container

  1. The container is not sorted, irrespective of the insertion position of the element with the value of the element.
  2. There vector, deque, list three kinds
(1). vector
头文件   #include<vector>
  • Dynamic arrays. Continuous elements stored in memory. Any element of a random access can be completed in a constant time. inEnd of the additions and deletions elementHaving a better performance (in most cases the time constants.
(2). and
头文件   #include<deque>
  • Two-way queue. Continuous elements stored in memory. Any element can be in a random access completion time constant (but substantially below vector). inAdditions and deletions at both ends of the elementsHaving a better performance (in most cases the time constant).
(3). list
头文件   #include<list>
  • Doubly linked list. Discontinuous elements stored in memory. inAdd or delete elements in any positionIt can be completed in time constant.It does not support random access

(B) The association container Introduction

  1. Element is the sort of
  2. Inserting any element, are by sort order to determine the appropriate position
  3. It has a very good performance when looking up
  4. Generally balanced binary manner, the insertion and retrieval time is O (log (N))
(1).set / multiset
头文件   #include<set>
  • That set collection. Allowed the same elements in this set, multiset allowed on the same element.
(2).map / multimap
头文件   #include<map>
  • Difference map and set ofIn that the element stored in the map and still only two member variables of a first, the other is called SECOND, map elements of small to large values ​​according to the first, and quickly retrieving elements according to first.
  • map with different multimapMap element that does not allow the same first two values, but can multimap.

(C) The container adapter Introduction

(1). stack
头文件   #include<stack>
  • Stack. It is a finite sequence of items, and meet the deleted sequence, retrieve and modify items can only be inserted into the sequence of recent items (top of the stack of items).After the backward
(2). queue
头文件   #include<queue>
  • queue. Can only be inserted in the tail, delete, retrieve and modify only from the head.FIFO
(3). priority_queue
头文件   #include<queue>
  • Priority queue. Highest priority element is always the first out of the column
(4) The order of the associated container and the container has a member function
begin  返回指向容器中第一个元素的迭代器
end    返回指向容器中最后一个元素后面的位置的迭代器 
rbegin 返回指向容器中最后一个元素的迭代器 
rend   返回指向容器中第一个元素前面的位置的迭代器  
erase  从容器中删除一个或几个元素
clear  从容器中删除所有元素 
(5) commonly used in a sequential container member functions
front       返回容器中第一个元素的引用
back        返回容器中最后一个元素的引用
push_back   在容器末尾增加新元素
pop_back    删除容器末尾的元素
erase       删除迭代器指向的元素(可能会使该迭代器失效),或删除一个区间,返回被删除元素后面的那个元素的迭代器 

IV. Iterator

(A) The concept

  1. A container element refers to the order and associated containers
  2. Iterators and pointers similar usage
  3. There are two kinds of const and non-const
  4. It points to the element can be read by the iterator
  5. You can modify its element pointed to by non-const iterator
  6. Iterator class defines a container of the method may be:
    容器类名::iterator 变量名;or容器类名::const_iterator 变量名;
  7. To access an iterator pointing to the element:* 迭代器变量名
  8. The iterator can perform ++ operatorSo that it points to the next element in the container. If the iterator is reached after the last element in the container, then use it at this time, an error occurs, similar to the use as a pointer or uninitialized BULL.
  9. Iterator example:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> v;   //一个存放int元素的数组,一开始里面没有元素
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	vector<int>::const_iterator i;  //常量迭代器
	for(i=v.begin();i!=v.end();++i)
		cout << *i << ",";
	cout << endl;
	vector<int>::reverse_iterator r;  //反向迭代器
	for(r=v.rbegin();r!=v.rend();r++)
		cout << *r << ",";
	cout << endl;
	vector<int>::iterator j;  //非常量迭代器
	for(j=v.begin();j!=v.end();j++)
		*j = 100;
	for(i=v.begin();i!=v.end();i++)
		cout << *i << ",";
	cout << endl;
	return 0;
 } 

operation result:
Here Insert Picture Description

(B) bidirectional iterators

If p and p1 are bidirectional iterators can be of p, p1 can do the following:

++p,p++       使p指向容器中下一个元素 
--p,p--       使p指向容器中上一个元素
*p             取p指向的元素
p=p1           赋值
p==p1,p!=p1   判断是否相等,不等  

(C) random access iterators

If p and p1 are random access iterators, you can pair p, p1 can do the following:

·双向迭代器的所有操作
·p += i        将p向后移动i个元素
·p -= i        将p向前移动i个元素
·p + i 值为:  指向p后面的第i个元素的迭代器
·p - i 值为:  指向p前面的第i个元素的迭代器
·p[i]  值为:  p后面的第i个元素的引用
·p < p1, p <= p1, p > p1, p >= p1

(D) The container iterator class

Here Insert Picture Description

V. Introduction to Algorithms

(A) The concept

  1. Algorithm is oneFunction templateMost defined in.
  2. STL can be provided in a variety of containers in common algorithms, such as search, sort the like.
  3. algorithmThrough the iteratorManipulating container elements. Many algorithms can be of a partial section of the vessel operation, and therefore requires two parameters, a start iterator element, a termination element is behind an iterator element. Such as sorting and searching.
  4. Some algorithm returns an iterator. For example find () algorithm, to find an element in the container, and returns a pointer to the iterator element.
  5. Algorithm can handle containers can also be handled ordinary arrays.

(B) example of an algorithm: find ()

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	int a[10]={10,20,30,40};
	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	vector<int>::iterator p;
	p=find(v.begin(),v.end(),3);
	if(p!=v.end())
		cout << *p << endl;
	p=find(v.begin(),v.end(),9);
	if(p == v.end())
		cout << "not found" << endl;
	p=find(v.begin()+1,v.end()-2,1);
	if(p!=v.end())
		cout << *p << endl;
	int *pp = find(a,a+4,20);
	cout << *pp << endl;
	return 0;
 } 

operation result:
Here Insert Picture Description

Published 21 original articles · won praise 25 · views 4021

Guess you like

Origin blog.csdn.net/So_cute_SJM/article/details/104083396