When calling the copy function, make sure that the target container is large enough, for example:
// Copy all elements of VEC to Coll. begin () is the starting address location copy (VEC. begin (), VEC. end (), Coll. begin ());
If you have not allocated enough memory for the Coll, an out-of-bounds error will occur.
If we cannot pre-allocate memory for Coll in advance, use the following code:
// Copy all elements of VEC to Coll. begin () is the starting address location copy (VEC. begin (), VEC. end (), back_insert (Coll. begin ()));
Back_insert is a type of insert iterator. The insert iterator helps us block the inserted element details so that ITER always points to an "available location ".
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_insert and front_insert is as follows:
1 #ifndef ITERATOR_H 2 #define ITERATOR_H 3 #include <iterator> 4 5 //BackInsertIterator 6 template <typename Container> 7 class BackInsertIterator 8 { 9 public:10 typedef typename Container::value_type value_type;11 12 explicit BackInsertIterator(Container &cont)13 :_cont(cont)14 { }15 16 BackInsertIterator<Container> &operator= (const value_type &val)17 {18 _cont.insert(_cont.end(), val);19 return *this;20 }21 22 BackInsertIterator<Container> &operator*()23 { return *this; }24 25 BackInsertIterator<Container> &operator++()26 { return *this; }27 28 BackInsertIterator<Container> &operator++(int)29 { return *this; }30 private:31 Container &_cont;32 };33 34 template <typename Container>35 BackInsertIterator<Container> backInsert(Container &c)36 { return BackInsertIterator<Container>(c); }37 38 39 //FrontInsertIterator40 template <typename Container>41 class FrontInsertIterator42 {43 public:44 typedef typename Container::value_type value_type;45 46 explicit FrontInsertIterator(Container &cont)47 :_cont(cont)48 { }49 50 FrontInsertIterator<Container> &operator= (const value_type &val)51 {52 _cont.insert(_cont.begin(), val);53 return *this;54 }55 56 FrontInsertIterator<Container> &operator*()57 { return *this; }58 59 FrontInsertIterator<Container> &operator++()60 { return *this; }61 62 FrontInsertIterator<Container> &operator++(int)63 { return *this; }64 private:65 Container &_cont;66 };67 68 template <typename Container>69 FrontInsertIterator<Container> frontInsert(Container &c)70 { return FrontInsertIterator<Container>(c); }71 72 #endif
Iteration adapter (1) Simple implementation of back_insert and front_insert