c ++ STL_2 container iterators fail?

remove and erase functions

In total: remove function delete elements, the need for careful use.
Description of algorithm: was the first to find a position of the element, and from this position traversing container, the latter elements are sequentially moved ahead, and valueelements of the same value. And finally returned iterator is pointing to the last valid element.
Look out !: the removealgorithm process, and does not modify the original containers size(), and end()other functions. Because when there is no concrete remove()is a function of a container which he will not change the value of the correlation coefficient of the original container, unless explicitly so as to use list.remove(1)and so on.
From a logical point of view, the last [from removeobtained iterator- _result, the end of the container end()) elements inside this interval has no meaning.

Such as the following realization:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class _ForwardIterator, class T>
_ForwardIterator Remove (_ForwardIterator first, _ForwardIterator last, const T& val)
{
  _ForwardIterator result = first;
  while (first!=last) {
    if (!(*first == val)) {
      *result = move(*first);
      ++result;
    }
    ++first;
  }
  return result;
}
int main()
{
	typedef  vector<char> Vector;
	Vector vec;
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('A');
	Vector::iterator p = Remove(vec.begin(),vec.end(),'A');
	cout << vec.end() - p << endl;
	for(Vector::iterator ite = vec.begin() ; ite != vec.end() ; ++ite)
	   cout << *ite << " " ;
	return 0 ;
}

Implementation of the results
Here Insert Picture Description

or

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	typedef  vector<char> Vector;
	Vector vec;
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('A');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('C');
	vec.push_back('A');
	Vector::iterator p = remove(vec.begin(),vec.end(),'A');
	cout << vec.end() - p << endl;
	for(Vector::iterator ite = vec.begin() ; ite != vec.end() ; ++ite)
	   cout << *ite << " " ;
	return 0 ;
}

The effect of the implementation of
Here Insert Picture Description
the effect of both is the same, it is because resultmaking records when screening starts iterator is pointing to first, then, as an example A A A A A C C C C C A, a five behind Cafter screening out, this time to stay at the equivalent iterator A A A A A C C C C C Ain the first Cplace, then so when the output result, not only have screened out the results C C C C C, there will be original list A A A A A C C C C C Ain the first Cand the elements behind C C C C C A, after it is output C C C C C C C C C C A. To avoid this effect occurs so frequently in the back fill vec.erase(remove(vec.begin(),vec.end(),'A'),vec.end());to remove unwanted subsequent data.
So remove()just after the element to be moved to remove elements vectorof the front end, instead of deleting. To actually remove the need to use with erase().

Then use the erase()function also pay attention to the point:

	for(ite=vec.begin();ite!=vec.end();){
		if(*ite == 'A')
			vec.erase(ite);
	//	else 
		//	++ite;
	} 

Such use is wrong, because erase()then, the field guide is ITE
Here Insert Picture Description
this case does not delete the following consecutive equal values, since erase()the return of the next element to be removed iterator:

	for(ite=vec.begin();ite!=vec.end();){
		if(*ite == 'A')
			ite = vec.erase(ite);
	//	else 
		//	++ite;
	} 

So what common form

for (ite = vec.begin(); ite != vec.end(); ) {
		if (*ite == 'A')
			ite = vec.erase(ite);
		else 
			++ite;
	}

Overview about the failure of other iterators:
vector

  1. vectorContainer, when the insert (push_back) an element, end()operation returns iterators failure, because he will update the container, expand the container.
  2. When the delete operation ( erase(), pop_back()after), point to point iterator remove all fail; delete reference to the element behind the point iterator will no longer work. In fact, this is also very good understanding, have been deleted, the deleted point iterator certainly fail ah, because the vector container is dynamic resizing, all elements are removed, then the elements behind affirmation forward ah.
  3. When the insert (push_back) an element, as compared with the return value of Capacity until there are no intervening elements change, the need to reload the entire container. The first end and the operation returns to the iterator will fail, this should be a reason for expansion.

deque

  1. In the header or tail deque container such that the insert element does not fail any iterators.
  2. In its first section the tail removing elements or element to be removed only make point iterator fails, as described above, the one example.
  3. Insert and delete operations in any other position of the container deque container element will point to the failure of all iterators. Because the queue element is a two-way relationship between adjacent relationship, rather than connected by pointers.

list
Will not increase any element iterator failure. When deleting an element, in addition to the current iterator element to be removed, the other iterator will not fail, because the pointer is to rely on the removed elements connected, is deleted and inserted it will not affect adjacencies.

set
If the iterator is pointing element is deleted, the iterator fails. Any increase in other operating delete elements do not cause the failure of iterators. Because he is automatically sorted.

map
If the iterator is pointing element is deleted, the iterator fails. Any increase in other operating delete elements do not cause the failure of iterators. Because he is automatically sorted.


Some of the information has a reference network data.

Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104569271