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
, multimap
four containers.
set
set
The value of the internal elements of an automatic sorting, each element can appear only once, not allowed to repeat.
multiset
multiset
And set
the 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 insert
functions provided insertion element.
But I would also like to emphasize insert
function, and other uses in the container:
- Specified position before it "insert" element value val, and returns a pointer to the iterator element,
iterator insert( iterator it, const TYPE &val );
- Specified position before it "Insert" num element with value val
void insert( iterator it, size_type num, const TYPE &val );
- Specified position before it "into the" interval [start, end) of all the elements.
void insert( iterator it, input_iterator start, input_iterator end );
map
map
Element is present in the form of a pair of numbers, i.e. (key,value)
, each element in accordance with the key- key
sorted, and each key only once.
multimap
multimap
And map
similar, 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_pair
function is generated (key,value)
elements;
wherein the contents of pointer first
pointing key, second
pointing to the stored value.
Of course map
the 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] = 1
but map
the container is not arry[10]
, at this time will generate a new element, added to the vessel, ie arry[10] = 1
; the mulltimap
capacity 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 stack
the container following the LIFO
original value, i.e., LIFO, (stack).
queue
Normal queue, follow the FIFO
FIFO original value.
priority_queue
Priority 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 STL
object container all the elements, you can point to any position exists, the equivalent c language
of 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
, multiset
iterators 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::iterator
a read-write mode, can modify the values of the elements in the container;
container::const_iterator
a 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 ++pos
because 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
, deque
when, when it may increase or remove elements in a loop, taking into account issues iterators may be invalidated.
In addition, there is a reverse_iterator
that is a reverse iterator, often in rbegin()
, and rend()
with use.