Iterator adapter (1) Implementation of back_inserter and front_inserter

Source: Internet
Author: User

This article discusses the implementation of back_inserter and front_inserter.

When calling the copy function, make sure that the target container has sufficient space, for example:

// Copy all the elements of other to Coll. begin () is the starting address location copy (Other. begin (), other. end (), Coll. begin ());

If you have not previously allocated memory for the Coll, this will cause an out-of-bounds error.

What if we cannot pre-allocate memory? We can use the following code:

// Copy all the elements of other to Coll. begin () is the starting address location copy (Other. begin (), other. end (), back_inserter (Coll. begin ()));

We use a back_inserter, which is a type of insert iterator (you will see later that it is actually a function). What is the insert iterator?

We know that the iterator is an abstraction used to implement container operations. With the iterator, we traverse all containers in almost the same way. In other words, the iterator helps us block the details of container operations.

For element insertion, different containers have different operations, such as push_back and insert. The insert iterator helps us block the inserted element details,So that ITER always points to a "available location ".

The use of back_inserter is as follows:

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;template <typename T>void printElems(const T &t, const string &s = ""){    cout << s << " ";    for(typename T::const_iterator it = t.begin();        it != t.end();        ++it)    {        cout << *it << " ";    }    cout << endl;}int main(int argc, char const *argv[]){    vector<int> coll;    back_insert_iterator<vector<int> > iter(coll);    *iter = 1;    iter++;    *iter = 2;    ++iter;    *iter = 3;    printElems(coll);    back_inserter(coll) = 44;    back_inserter(coll) = 55;    printElems(coll);    copy(coll.begin(), coll.end(), back_inserter(coll));    printElems(coll);    return 0;}

We can see that the insert iterator is easy to use, and we don't need to consider the memory allocation, because the iterator handles these details internally. As mentioned above, the insert iterator always points to an available position and we will soon see its implementation details.

Pay attention to the following points:

1. The insert iterator is essentially an adapter, but it looks like an iterator and acts like an iterator, so it conforms to the definition of the iterator.

2. Insert iterator assignment. The insert element method is adopted internally. The push_back push_front or insert method of the container may be called.

3. Insert the ++ operation of the iterator,It's just a guise, But it's essential.. For example, the above copy function must call ITER ++ internally, because the copy function only treats it as a common iterator.

4. The unreference operation is also a guise.

The implementation code of back_inserter and front_inserter is as follows:

# Ifndef define # define <typename container> class backinsertiterator {public: typedef typename container: value_type; explicit backinsertiterator (container & Cont): cont _ (cont) {} backinsertiterator <container> & operator = (const value_type & Val) {cont _. insert (cont _. end (), Val); return * This;} backinsertiterator <container> & operator * () {return * This;} backinsertiterator <container> & operator ++ () {return * This;} // ITER ++ does not have any substantive operations. Therefore, backinsertiterator <container> & operator ++ (INT) {return * This;} PRIVATE: container & Cont _ ;}; template <typename container> backinsertiterator <container> backinserter (container & C) {return backinsertiterator <container> (c );} // frontinsertiteratortemplate <typename container> class frontinsertiterator {public: typedef typename container: value_type; explicit frontinsertiterator (container & Cont): cont _ (cont) {} frontinsertiterator <container> & operator = (const value_type & Val) {cont _. insert (cont _. begin (), Val); return * This;} frontinsertiterator <container> & operator * () {return * This;} frontinsertiterator <container> & operator ++ () {return * This;} frontinsertiterator <container> & operator ++ (INT) {return * This;} private: Container & Cont _;}; template <typename container> frontinsertiterator <container> frontinserter (container & C) {return frontinsertiterator <container> (c );}

 

From the source code above, we can see that the insert operation is used for both inserts. Of course,It is also possible to call push_back and push_front..

Iterator adapter (1) Implementation of back_inserter and front_inserter

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.