Introduce a very easy to use Overwrite iterator

zhaozj2021-02-16  60

Introduce a very easy to use Overwrite iterator

[Original title] C Tip # 11 Overwrite Iterator

[Source] C User Journal December 2002 Volume 20 Number 12

[Author] ray Virzi

[Translator] Easyjoy

[Keyword] iterator container copy

【Abstract】 This article introduces a new iterator (OverWrite_Iterator), which can be easily overwritten from a new element from a sequence without cleaning the original element of the container; and automatically grows the size of the container as needed.

【text】

If you want to copy a sequence to a container (Container), usually you use the std :: copy algorithm, the code is as follows:

Std :: Copy (Start, End, std :: back_inserter (container);

Here, start and end are an iterator (Iterator) of the input sequence (assuming N), and container is a container, the container interface contains a function Push_Back. Suppose the container begins to be empty, then it will contain N elements after COPY, and the order is the same as the order in the original queue. The Back_INSERTER template function provided by the standard library is very convenient because it returns a back_insert_iterator iterator for Container, so that the copied elements are appended to the end of the Container.

Now, it is assumed that the Container starts non-empty (for example: Container must be used repeated several times in the loop). So, to reach the original goal, you must call the CLEAR function before you can insert a new sequence. This will result in the old element object being destructed, and the newly added is constructed. Not only that, the dynamic memory used by Container itself will be released and created again, just like a node of List, Map, Set. Some vector's implementation even releases all memory even when calling Clear. Typically, it is more efficient to cover directly COPY on an existing element. Maybe you will do this:

Std :: Copy (start, end, container.begin ());

Here you perform a Copy-Over operation in the head of the Container, however, this code can cause crash if the size of the Container is smaller than the length n of the input sequence. Now I need a new iterator, this new iterator performs a copy-over operation before reaching the Container, then execute the APPEND operation. I named this iterator to OverWrite Iterator, which defines the code list 1; and follow the standard library practice, providing two auxiliary template functions to construct OverWrite Iterator from the specified container, see Code Listing 2.

Now use the auxiliary function, use the following code to reach the goal:

Std :: Copy (Start, end, overwriter);

If you want to start copying from a certain iterator of the container, you can use the following code:

Std :: Copy (Start, End, Overwriter (Container, IT));

This Copy Algorithm performs the result to return an OverWrite iterator, pointing to the next location of the element being copied in Container. Since this iterator has an implicit conversion (Implicit Conversion), it can be easily used to delete the remaining elements in the container (if there is this need), or put another sequence The element is copied and copied. So this solution is efficient, elastic, safe, and more important to use. [Code List 1] Overwrite_Iiterator's implementation

#include

Template

Class Overwrite_Iuterator

: Public std :: item {

PUBLIC:

TypeDef Container_Type;

Typedef Cont :: Value_Type Value_Type;

Explicit overwrite_iterator (cont & x)

: CONT (X), Iter (x.begin ())

{}

Overwrite_iterator (Cont & X, Cont :: Iterator IT)

: CONT (X), ITER (IT)

{}

Overwrite_iterator & operator = (Const Cont :: Value_Type & Val)

{

// i == cont.end ()? Cont.push_back (VAL): (* iter = VAL, iTer); THX to SKY1234

iter == cont.end ()? (Cont.push_back (val), ney = cont.end ()): (* iter = VAL, iTer; Return * this;

}

Overwrite_iterator & operator * ()

{

Return * this;

}

OverWrite_Iterator & Operator ()

{

Return * this;

}

Overwrite_iterator Operator (int)

{

Return * this;

}

Operator Cont :: Iterator ()

{

Return ITER;

}

protected:

CONT & CONT;

Cont :: Iterator.

}

[Code Listing 2] Assist function (helper function) Template

Overwrite_iterator overwriter (cont & x)

{

Return Overwrite_Iuterator (x);

}

Template

Overwrite_iterator Overwriter (Cont & X, Cont :: Iterator IT)

{

Return Overwrite_Iuterator (x, it);

}

Author's introduction:.

转载请注明原文地址:https://www.9cbs.com/read-25242.html

New Post(0)