c ++ STL_1 Container Overview

container

Sequence container

Their arrangement order and input order consistent, there are three commonly used in the STL sequence containers: vector, deque, list;

vector

Allowing random access, i.e. can directly access any element index, but more time-consuming access in the middle of the head and, in a high tail advantage is the additional element or removed element efficiency, such as:

#include<iostream>
#include<vector>//头文件
using namespace std;
int main()
{
	vector<int> arry;//空容器
	for(int i = 0;i <= 6;i ++)
		arry.push_back(i);
	for(int i = 0;i < arry.size();i++)
		cout<< arry[i]<<" ";
	return 0;
}

Wherein size()the sequence of functions consensus container;
can [ ]access the specified index two elements;

and

The so-called two-way queue, the tail and the head insert elements high efficiency, which is to his advantage, but insufficient element is inserted in the middle of time-consuming to achieve, such as:

#include<iostream>
#include<deque>//头文件
using namespace std;
int main()
{
	deque<int> arry;//空容器
	for(int i = 0;i <= 6;i ++){
		arry.push_back(i);
		arry.push_front(i);
	}
	for(int i = 0;i < arry.size();i++)
		cout<< arry[i]<<" ";
	return 0;
}

Wherein push_front()for inserting the head of the queue element;

list

That is doubly linked list, the advantage lies in the implementation highly efficient insertion and deletion at any position, the disadvantage is not random access;

#include<iostream>
#include<list>//头文件
using namespace std;
int main()
{
	list<int> arry;//空容器
	for(int i = 0;i <= 6;i ++)
		arry.push_back(i);
	while(!arry.empty()){
		cout<< arry.front()<<" ";//输出第一个元素但不执行删除操作
		arry.pop_front();//删除第一个元素但不返回被删除的元素
	}
	return 0;
}

Associative containers

Associative containers are generally realized by a binary tree, than the value of the parent node of the left subtree smaller than the value of the parent node and right node a large value. Common: set, multiset, map, multimapfour containers.

set

setThe value of the internal elements of an automatic sorting, each element can appear only once, not allowed to repeat.

multiset

multisetAnd setthe like, but allows duplicate values.
To achieve such as:

#include<iostream>
#include<set>
using namespace std;
int main()
{	
	typedef set<int> intset;//缺省排列,默认operaotr<规则
	intset arry;
	arry.insert(1);
	arry.insert(2);
	arry.insert(3);
	arry.insert(4);
	arry.insert(5);
	intset::const_iterator pos;
	for(pos = arry.begin();pos != end.();++pos)
		cout << *pos << " " ;
	return 0;
}

All containers are associated with insertfunctions provided insertion element.

But I would also like to emphasize insertfunction, and other uses in the container:

  1. Specified position before it "insert" element value val, and returns a pointer to the iterator element,
    iterator insert( iterator it, const TYPE &val );
  2. Specified position before it "Insert" num element with value val
    void insert( iterator it, size_type num, const TYPE &val );
  3. Specified position before it "into the" interval [start, end) of all the elements.
    void insert( iterator it, input_iterator start, input_iterator end );
map

mapElement is present in the form of a pair of numbers, i.e. (key,value), each element in accordance with the key- keysorted, and each key only once.

multimap

multimapAnd mapsimilar, but allows duplicate keys.
To achieve such as:

#include<iostream>
#include<map>
using namespace std;
int main()
{	
	typedef map<int,int> intmap;//缺省排列,默认operaotr<规则
	intmaparry;
	arry.insert(make_pair(1,1));
	arry.insert(make_pair(4,6));
	arry.insert(make_pair(2,4));
	arry.insert(make_pair(7,1));
	arry.insert(make_pair(6,9));
	intmap::const_iterator pos;
	for(pos = arry.begin();pos != end.();++pos)
		cout << pos->second << " " ;
	return 0;
}

Wherein the make_pairfunction is generated (key,value)elements;
wherein the contents of pointer firstpointing key, secondpointing to the stored value.

Of course mapthe container, in addition to the above forms, there is a direct assignment or generate elemental form, map may be a operaator[ ]wherein the subscript is the key, the value of the assignment is stored, also called associative arrays, such as:

#include<iostream>
#include<map>
using namespace std;
int main()
{	
	typedef map<int,int> intmap;//缺省排列,默认operaotr<规则
	intmaparry;
	arry[1] = 3;
	arry[3] = 1;
	arry[6] = 7;
	intmap::const_iterator pos;
	for(pos = arry.begin();pos != end.();++pos)
		cout << pos->second << " " ;
	return 0;
}

Using the above form, it is worth mentioning that when we insert a value as we arry[10] = 1but mapthe container is not arry[10], at this time will generate a new element, added to the vessel, ie arry[10] = 1; the mulltimapcapacity can not be used, because he will be in many cases , i.e. an index, corresponding to a plurality of stored values.


Container adapter

stack, queue, priority_queue
Wherein stackthe container following the LIFOoriginal value, i.e., LIFO, (stack).
queueNormal queue, follow the FIFOFIFO original value.
priority_queuePriority queue, the priority is determined by the ranking criteria elements, such as operator<i.e. to a criterion in ascending order, can be given priority.


Iterator

Iterator is traversing an STLobject container all the elements, you can point to any position exists, the equivalent c languageof a pointer, but then, in STL, we can not be viewed pointer iterators, pointers and memory bound, and iterator is the container element binding, then deleted, the iterator becomes ineffective, before its re-assignment, can no longer access this iterator.

Iterator divided into the following two categories:
** Bidirectional iterator: ** Bidirectional iterators can be two-way, have list, set, multimap, map, multisetiterators are such.
Random access iterators **: ** may be a random access.

There are commonly used in the iterator following operations:
operator *: Returns the value of the position of the current element;
operator ++: an iterator moves rearward;
operator == and operator !=Analyzing iterator point to the same location;
operator=: iterator assigned, i.e. to give him a position point, road signs?

Further, the iterator with several functions
begin(): starting point of the container;
end(): end-point of the container, i.e., the next position of the last element;
determining whether a container is empty, in addition to the previously described empty(),size(), there are begin() == end()equal is empty.

And any container iterator has two types:
container::iteratora read-write mode, can modify the values of the elements in the container;
container::const_iteratora read-only mode, the value of the element can not be modified container;

Also note that, when we use the iterator loop, the general pre-increment ++posbecause the rear also need a extra temporary object, this thing of c ++ programming experience students should know and shape as it achieved

container operarot++ (int)(){
	container a(this);
	++this;
	return a;
}

When you are dealing with vector, dequewhen, when it may increase or remove elements in a loop, taking into account issues iterators may be invalidated.
In addition, there is a reverse_iteratorthat is a reverse iterator, often in rbegin(), and rend()with use.

Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104564353