Insert iterators in C + + and their iterator adapters

Source: Internet
Author: User

There are three types of iterators in C + +, namely Insert iterators (Inserter), reverse iterators (reverse_iterator), and Flow iterators.

Here (vs2003 for example) introduces the insertion iterator, which is the Std::inserter_iterator,std::back_inserter_iterator,std::front_inserter_iterator three classes, respectively, The corresponding iterator adapter should be

Std::inserter,std::back_inserter,std::front_inserter.

1. Std::inserter_iterator (Std::inserter)

TEMPLATE FUNCTION Inserter
Template<class _container,
Class _iter> Inline
Insert_iterator<_container> Inserter (_container& _cont, _iter _where)
{//Return Insert_iterator
Return (std::insert_iterator<_container> (_cont, _where));
}
This is the normal insertion of the Delle adapter, and we see that the function needs to specify the container type and the container's start pointer (insert from that position) to return an iterator constructed by container and container iterator position. Let's illustrate this with a call example.

Std::vector<std::string> V1,v2;
Std::copy (V1.begin (), V1.end (), Std::inserter (V2,v2.begin ()));

This indicates that all elements of the V1 need to be inserted into the V2. The timing of the call is to construct a Std::inserter instance a (named a), and then through the loop inside the Std::copy, constantly call adapter A's assignment operation symbol, assigning the elements inside the V1 to v2. Let's look at the realization of Std::insert_iterator,

Template<class _container>
Class Insert_iterator
: Public _outit
{//Wrap inserts into container as output iterator
Public
typedef _container CONTAINER_TYPE;
typedef typename _container::reference Reference;

Insert_iterator (_container& _cont, TypeName _container::iterator _where)
: Container (&_cont), ITER (_where)
{//construct with container and iterator
}

insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//INSERT into container and increment stored iterator
iter = Container->insert (iter, _val);
++iter;
return (*this);

}

//... Omitted the other functions of the Insert_iterator

See

insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//INSERT into container and increment stored iterator
iter = Container->insert (iter, _val);
++iter;
return (*this);
}

Here, by passing in the container and the container's iterator, the instance is constructed first, and the assignment operator actually invokes the Container->insert method of the container, so there must be a container with the Insert method. Fortunately, almost all containers have insert methods, so this is a generic iterator. This insert method must also be similar to this declaration:

Iterator Insert (iterator _where, const _ty& _val);

Returns an iterator that passes in the location and value.

2. Std::back_inserter_iterator (Std::back_inserter)

As the name suggests, Back_inserter is inserted at the back of the container, the actual back_inserter implementation and Inserter basically the same, because it is specified at the end of the insert, so do not need to specify the insertion of easy start iterators;

Template<class _container> Inline
Back_insert_iterator<_container> Back_inserter (_container& _cont)
{//return a Back_insert_iterator
Return (std::back_insert_iterator<_container> (_cont));
}

Let's look at the realization of Std::back_insert_iterator,

TEMPLATE CLASS Back_insert_iterator
Template<class _container>
Class Back_insert_iterator
: Public _outit
{//wrap pushes to back of container as output iterator
Public
typedef _container CONTAINER_TYPE;
typedef typename _container::reference Reference;

Explicit Back_insert_iterator (_container& _cont)
: Container (&_cont)
{//Construct with container
}

back_insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->push_back (_val);
return (*this);
}

//... Omitted the other functions of the Back_insert_iterator

See

back_insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->push_back (_val);
return (*this);
}

We also use a call to see

Std::vector<std::string> V1,v2;

Std::copy (V1.begin (), V1.end (), Std::back_inserter (v2));

Here, by passing in the container, the instance is constructed first, and the assignment operator actually invokes the Container->push_back method of the container, so there must be a push_back method for the container, so this pair of associative containers STD::MAP,STD: The Muitlmap is invalid.

3. Std::front_inserter_iterator (Std::front_inserter)

As the name suggests, Front_inserter is inserted in the front of the container, the actual front_inserter implementation and Back_inserter basically the same, because it is specified in the front inserts, so do not need to specify the insertion of easy start iterators;
TEMPLATE FUNCTION Front_inserter
Template<class _container> Inline
Front_insert_iterator<_container> Front_inserter (_container& _cont)
{//Return Front_insert_iterator
Return (std::front_insert_iterator<_container> (_cont));
}

Let's look at the realization of Std::front_insert_iterator,
TEMPLATE CLASS Front_insert_iterator
Template<class _container>
Class Front_insert_iterator
: Public _outit
{//wrap pushes to front of container as output iterator
Public
typedef _container CONTAINER_TYPE;
typedef typename _container::reference Reference;

Explicit Front_insert_iterator (_container& _cont)
: Container (&_cont)
{//Construct with container
}

front_insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->push_front (_val);
return (*this);
}

//... Omitted the other functions of the Front_insert_iterator

See
front_insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->push_front (_val);
return (*this);
}

We also use a call to see
Std::vector<std::string> V1,v2;
Std::list<std::string> list_1,list_2;
Std::copy (V1.begin (), V1.end (), Std::front_inserter (list_2));
Here, by passing in the container, the instance is constructed first, and the assignment operator actually invokes the Container->push_front method of the container, so this requires the container to have a Push_front method, so this pair of associative containers STD::MAP,STD: The Muitlmap,std::vector are useless,
This is more effective for std::d eque,std::queue,std::list This sort of sequencing.

1.4. Summary

In general, the three-class iterators are an output iterator, since they all have a _outit output iterator. Various containers for different implementations, three iterators are using the operator= () This assignment operation symbol to implement the insertion, through the operator* () to take out the value.

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.