C ++ iterator containers and (b)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/baiyibin0530/article/details/90642114

vector class RAII

template<typename T, typename A>

Vector void <T, A> :: Reserve (int NEWALLOC)
{
    IF (NEWALLOC <= Space) return; // never decreases allocated memory
    T * p = alloc.allocate (newalloc) ; // allocate new space
    for ( int i = 0; i <sz ; ++ i) alloc.construct (& p [i], elem [i]); // copy
    for (int i = 0; i <sz; ++ i) alloc.destroy ( & elem [i]); // destruction
    alloc.deallocate (elem, space); // release the old space
    elem = P;
    space = NEWALLOC; 
}


A simple text editor


The most important is the nature of the list can be inserted or deleted without moving elements.


We text file as a series of "line" component, and is represented by list <Line>, where Line is a vector <char>.

Can add a new list in the list without moving an existing link is very important, for reasons of logic is that we may
now using iterator to these existing links, or pointer (and references) point to these links the object.

Processing line

How should we determine what the document is "line" mean? Obviously, there are three choices:
1, by a newline character (e.g. ', n') is determined.
2, using some method for analyzing a document with some kind of "natural" punctuation (eg.).
3, all over a given length (e.g., 50 characters) of the row into two rows.

using Line = vector <char>; // line is a character vector

the Document struct
{
    lsit <Line> Line; // row of a document is a List
    the Document () {line.push_back (Line {});}
};

Reading a document and branches can be done as follows:

& operator >> the istream (the istream & IS, the Document & D)
{
    for (CH char; is.get (CH);)
    {
        d.line.back () push_back (CH);. // add characters
        if (ch == '\ n-')
            d.line.push_back (line {}); // add a line
    }
    IF (d.line.back () size ()) d.line.push_back (line {});. // add the last empty OK
        return iS;
}
Vector list and has a back () member function returns a pointer to the end of the reference element.


Traversal

We define a special class for the Document iterator:


class Text_iterator // trace line and the line character position
{
    List <Line> :: Iterator the In;
    Line :: Iterator POS;

public:
    // iterator character line starts at the position PP ||
    Text_iterator (List <Line> :: || Iterator, Iterator :: Line PP)
        : LL the In {}, {}} PP {POS

    char& operator&*(){reurn *pos;}
    Text_iterator& operator++();
    bool operator==(const Text_iterator& other) const
        {return In==other.In && pos==other.pos;}
    bool operator != (const Text_iterator& other) const
        {reurn !(*this==other);}
};


vector, list and string
four methods of storing the character sequence
char [] (character array);
Vector <char>;
string;
List <char>

elem []: does not know its own size.
Vector [elem]:
String: which ensure continuous element stored in memory. string can be extended.
list [elem]: 

If you need to use a lot of large objects in the program, and will point to them in many places (with iterator or pointer), you should consider using list.

The difference is that the list of vector, we did not move any element, q is always effective.

list <char> and the other three containers at least 3-fold compared ---- storage on a PC, a list <char>
12 bytes to store each element, the vector <char> only takes 1 bytes.

In fact, one of vector design ideas is to push_back () such a "memory operation" to optimize,
and string are not.

The main difference between vector and string logic is that the vector can be used for almost any type of element, but only when dealing with characters we
only consider the string .. In short, only when the need for string manipulation before considering the use of string, in other cases, is a good vector.


Adjustment to achieve the STL vector class version of the function


Adjust the built-in array to achieve the STL version features


Tank At a Glance


STL provides a number of containers:
the sequence of elements vector stored contiguously; the default container applications do
list doubly linked list; when you want to use to insert and delete elements done without moving the existing elements of
cross-vector and the deque list; unless you are very proficient in arithmetic and knowledge of computer architecture, do not use it to
map a balanced and orderly tree; use it when you need to access elements by value.
multimap equilibrium ordered tree can contain multiple copies of the same key; when you need to use it as the value of the access element.
unordered_map hash table; an optimized map; use it as a large-scale maps, for high performance requirements and can design a good hash function when
unordered_multimap 
the SET balanced and orderly tree; use it when you need to track a single value
multiset may contain multiple copies of the same key balance of the ordered tree; if you need to use it to track a single value is
a fixed size array array, the array built most of the problems that exist absent

What is a container?
A STL container:
is a sequence of elements [begin (): end () ].
Copy operation providing a copy of the elements. Copy may be realized by the assignment operator or copy constructor.
The element type named value_type.


Iterator class

Input iterator: we can move forward with ++, with * read element values. provided that such istream iterators.
Output iterator: we can move forward with ++, with * write element values. provided that such ostream iterator.
Forward iterators: we can repeatedly move forward with ++, with * read and write elements.
Bidirectional iterators: ++ we can use repeatedly to move forward with - move backward, with * read and write elements.
Random access iterators: ++ we can use repeatedly to move forward with - move backward, use * or [] to read and write elements.
We can carry out operations on the index random access iterators, use the + plus an integer to the iterator, with - minus an integer to the iterator.
We can get them by pointing to the same sequence from two iterator subtraction. provided that such vector iterator.


 

Guess you like

Origin blog.csdn.net/baiyibin0530/article/details/90642114