C + + adapter (Adapter) Summary

Source: Internet
Author: User

Recently in the "STL Source Code Analysis", STL inside a lot of generic programming is really clever, but because of limited time, I can only more focus on daily use more knowledge.

Adapter is I at the beginning of the use of STL container when I heard a word, has not been more in-depth understanding, through this study STL source, summed up here:

First is the definition of adapter, "Design Patterns" for the definition of adapter I feel really very accurate: the interface of a class is converted to another class interface, so that the interface is incompatible with the class can not co-operation together. This is straightforward enough to make me think of the common adapter in everyday life: power adapters. Similarly, the effect of both is similar.

There are two main types of adapters (Adapter): The container adapter (Container Adapter) and the iterator adapter (Iterator Adapter), and here are a few examples of the two main types of Adapter:

Container Adapter:

The most commonly used container adapters are queue and stack, in the sense that they provide a layer of packaging for the underlying container, so that the underlying container only shows the functionality of the data structures defined by these adapters.

This assumes that the underlying container is deque, two-way openings, and that both ends of the insert Delete can achieve an O (1) time complexity. When wrapped as a queue, deque will close one end of the insert operation, close the other end of the delete operation, so that the entire deque show the characteristics of the FIFO, in the same time as a stack.

Can see container adapter do the function is very intuitive, but also very good understanding, just for the container of a layer of packaging, changed the interface of the data structure while facilitating our understanding and use of it.

Iterator Adapter (#include <iterator>):

The iterator adapter I was not very familiar with, for the use of iterator in STL I would probably be the most of begin (), End (), Const_iterator,rbegin () and so on, and here I'm going to talk about iterator There are three main types of adapter, namely insert Iterator,reverse Iterator,iostream iterator.

The first is insert iterator. By definition, it works by turning the assignment (assign) operation of a generic iterator into an insert operation. Understanding this sentence also understood the essence of insert iterator.

The main usage of insert iterator in STL is shown:

The biggest difference in these three forms is the position of the element inserted after the adapter is used.

Let's start with the simplest test code:

1#include <iostream>2#include <iterator>3#include <vector>4 using namespacestd;5 6 intMain () {7vector<int> data={6,2,3,4,5,9};8      for(inti =0;i<Ten;++i) {9*back_inserter (data) =i;Ten     } One  A      for(Auto J:data) cout<<j<<" "; -cout<<Endl; -     return 0; the}
Output results: 6 2 3 4 5 9 0 1 2 3 4 5 6 7 8 9

As you can see, we can still think of the result after iterator adapter modification as an iterator that can still be used with the * symbol, but the difference is that when you assign it, it will automatically insert. It should be better to use the description in the original book:

That is, in the case of insert iterator, the iterator simply turns the assign operation into Push_back,push_front,insert, while the other operations + +,--, * All return to the original iterator

This will also be able to explain the copy function when copying the underlying implementation, after each copy of a value of + + for the Insert iterator does not work, only the assignment assign will play a related action.

When using the copy function:

1#include <iostream>2#include <iterator>3#include <vector>4#include <algorithm>5 using namespacestd;6 7 intMain () {8vector<int> data={6,2,3,4,5,9};9vector<int> res={2,1,1};Ten copy (Data.begin (), Data.end (), Back_inserter (res)); One      for(Auto J:res) cout<<j<<" "; Acout<<Endl; -     return 0; -}
Output results: 2 1 1 6 2 3 4 5 9

Copy always inserts a number from the end of the Res and makes a copy, rather than just the assignment that exists in copy's original meaning.

Reverse iterator:

The obvious effect is to make iterator in the opposite direction, often used with rbegin,rend, such as:

1#include <iostream>2#include <iterator>3#include <vector>4#include <algorithm>5 using namespacestd;6 7 intMain () {8vector<int> data={6,2,3,4,5,9};9vector<int>::reverse_iterator it =Data.rbegin ();Ten      for(; It!=data.rend (); ++it) cout<<*it; Onecout<<Endl; A     return 0; -}
Output: 954326

Iostream iterator:

This adapter is associated with the iterator and input and output streams (which can be file streams) and is often used in conjunction with the copy function:

1#include <iostream>//Std::cout2#include <iterator>//Std::ostream_iterator3#include <vector>//std::vector4#include <algorithm>//std::copy5 6 intMain () {7std::vector<int>Myvector;8    for(intI=1; i<Ten; ++i) Myvector.push_back (i*Ten);9 Tenstd::ostream_iterator<int> Out_it (std::cout,", "); One std::copy (Myvector.begin (), Myvector.end (), out_it); A   return 0; -}
Output:tenand a

As can be seen from the above example, the adapter (Adapter) plays an important role in STL, which is closely related to the common functions of copy and so on, and can be combined with the input and output operation.

Reference: "STL Source Code Analysis"

http://www.cplusplus.com/reference/iterator/ostream_iterator/

C + + adapter (Adapter) Summary

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.