Efficient STL-iterator & amp; algorithm, STL-generator

Source: Internet
Author: User

Efficient STL-iterator & algorithm, STL-generator

Efficient STL-iterator & Algorithm

Each standard STL container provides four types of iterators: iterator, const_iterator, reverse_iterator, and const_reverse_iterator. At the same time, some forms of insert and erase of the container only accept one of them.

There is no way to convert from the const iterator to a non-const iterator, neither implicit nor const_case conversion. However, you can use advance and distance.

Advance (I, distance (I, ci ));

// I is a general iterator, and ci is a const_iterator

To obtain the iterator that points to the same position as const_iterator, first point iterator to the starting position of the container, and then move it forward to the same position as the offset between const_iterator and the starting position of the container. This task is assisted by two function templates, advance and distance, which are declared in <iterator>. Distance returns the Distance between two iterator pointing to the same container. advance is used to move an iterator to the specified Distance. (In actual calls, you may need to explicitly specify the template parameter type called by distance. How to specify the template parameter !)

 

For reserve_iterator member functions, "corresponding" iterator can be generated.

To insert a new element in the position specified by a reverse_iterator. base () points to the position to insert the line, for the insert operation, ri and ri. base () is equivalent, and ri. base () is the iterator corresponding to ri.

To delete an element at the position specified by reverse_iteratorri, delete the previous element of ri. base. For delete operations, ri and ri. base () are not equivalent, and ri. base () is not the iterator corresponding to ri.

At the same time, you must understand the reverse iterator of reserve_iterator. Its base () member function returns an element on its right.

 

To delete an element from a container, remove is similar to the algorithm. to delete an element from a container, you can only call its member functions, because remove cannot know the container it is operating on, therefore, removing cannot remove elements from a container. Removing an element from a container does not change the number of elements in the container. If you want to delete elements in the container, you need to call the erase function after you call remove.

 

Some algorithms require ordered intervals:

Not all algorithms can be used in any range. Removing requires the ability to forward the iterator and assign values through these iterators. Therefore, it cannot apply the intervals divided by the input iterator or map or multimap. It cannot be the implementation of set or multiset. Many sorting algorithms require random access to the iterator, so it is impossible to call these algorithms on a List element. Some algorithms that require Ordered Space include:

Binary_search lower_bound upper_bound interval _range set_union set_intersection set_diffence

 

Mem_fun and mem_fun_ref. You must use them as long as you pass a member function to the STL component.

 

Use a member function instead of an algorithm with the same name.

Some containers have member functions with the same name as STL algorithms. The associated container provides count find lower_bound upper_bound limit _range, while the List provides remove remove_if unique sort merge reverse.

 

The behavior of List member functions is often different from that of their algorithm brothers. If you want to clear objects from the container, call the remove remove_if unique algorithm and then call the erase function. However, the removeremove_if and unique member functions of the List really remove the elements, you do not need to call erase later. An important difference between the sort algorithm and the sort member function of the list is that the former cannot be used as a simple bidirectional iterator, And the List iterator cannot be passed to the sort algorithm. There is also a huge difference between the Merge algorithm and the merge member functions of the list. When selecting between STL algorithm and container member functions with the same name, you should try to use member functions.

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.