[STL] Insert iterator or inserter)

Source: Internet
Author: User

1. Definition
The insert iterator is also called the inserter ).
2. Role
The main function of inserting an iterator is to convert a value assignment operation into an operation that inserts the corresponding value into the container.
The algorithm library imposes restrictions on all operations on the container: Never modify the container size (no insert or delete ). With the plug-in iterator, the algorithm library can insert new elements to the container through the iterator without violating this uniform band, that is, the design consistency is maintained.
3. Type
3.1 back_insert_iterator)
Use: Call the push_back () member function of the container to insert an element.
Function: Insert elements at the end of the container.
Restriction: Only containers that provide push_back () member functions
Applicable: vector deque list
3.2 front_insert_iterator)
Use: Call the push_front () member function of the container to insert an element.
Function: Insert elements at the front end of the container.
Restriction: Only containers that provide push_front () member functions
Applicable: deque list
3.3 insert_iterator)
Use: insert an element by calling the insert () member function, and specify the insert position.
Function: insert an element at the specified position of the container.
Restriction: All STL containers provide the insert () function.
Applicable: All STL containers
4. Example

# Include <iostream> # include <vector> # include <list> # include <iterator> using namespace STD; Template <typename T> void printelements (t C) {T :: const_iterator itr = C. begin (); While (itr! = C. end () cout <* itr ++ <"" ;}int main () {vector <int> vecsrc; List <int> vecdest; for (vector <int >:: size_type I = 0; I <3; ++ I) vecsrc. push_back (I); // 1. class handler and function back_inserter // explicit back_insert_iterator (container & _ cont); // template <class container> back_insert_iterator <container> back_inserter (container & _ cont); copy (vecsrc. begin (), vecsrc. end (), back_insert_iterator <list <int> (vecdest); // copy (vecsrc. begin (), vecsrc. end (), back_inserter (vecdest); // printelements (vecdest); cout <Endl; // 2. class delimiter and function front_inserter // explicit front_insert_iterator (container & _ cont); // template <class container> front_insert_iterator <container> front_inserter (container & _ cont); copy (vecsrc. begin (), vecsrc. end (), front_insert_iterator <list <int> (vecdest); // copy (vecsrc. begin (), vecsrc. end (), front_inserter (vecdest); printelements (vecdest); cout <Endl; // 3. class insert_iterator and function inserter // insert_iterator (container & _ cont, typename container: iterator _ it ); // template <class container> insert_iterator <container> inserter (container & _ cont, typename container: iterator _ Where); copy (vecsrc. begin (), vecsrc. end (), insert_iterator <list <int> (vecdest, ++ vecdest. begin (); // copy (vecsrc. begin (), vecsrc. end (), inserter (vecdest, ++ vecdest. begin (); printelements (vecdest); Return 0 ;}

Running result:

0 1 22 1 0 0 1 22 0 1 2 1 0 1 2 press any key to continue.

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.