Rereading STL source code analysis: vector

vector

data structure:

Three data members:

iterator start; // head pointing vector space currently used, i.e. vector.begin ()

iterator finish; // End point vector space currently used, i.e. vector.end ()

iterator end_of_storage; // End point vector space currently available

Important functions:

begin():return start;

end():return finish;

size():return size_type(end()-begin());//size_type类似于value_type

capacity():return end_of_storage-start;

resize():

First determines whether the new size is smaller than the original size, if the size is less than (), the elements will be erased after new_size

Otherwise, the end filled the missing element (after)

void resize(size_type new_size,const T&x)
{
	if(new_size<size())
		erase(begin()+new_size,end());
	else
		insert(end(),new_size-size(),x);
}

clear (): erase (begin (), end ()); // only destructor, space is not released  

push_back():

First determines whether space is available, if available, then the configuration at the finish, and finish the shift;

If not, the call insert_aux move, following specific description insert_aux ();

void push_back(const T& x)
{
	if(finish!=end_of_storage){
		construct(finish,x);
	++finish;
	}
	else
		insert_aux(end(),x);
}

  

insert_aux()

Code P122 page

specific process:

First, there is determined whether the current capacity (finish! = End_of_storage), if there is capacity, then [position, finish) a shift element of the whole, inserting a new element in the original position, while the finish moved back.

If there is no capacity, the new space is first calculated (the original 2-fold), and New dimensions corresponding to the size.

After completion of the original open elements copied into the new space, copy destructor source element after the completion of the space and free space, re-adjust the three iterator start, finish and end_of_storage

 

pop_back()

pop_back () just forward and destructor original finish back () element does not release the space

void pop_back()
{
  --finish;
  destory(finish);
}

  

erase()

To delete the part of the [first, last), the first copy [last, finish) to the first element and then, and then clear the area after covering elements, that is, [i, finish)

The finish is then moved to the end position of the cover, the element returns the new insertion position

iterator erase(iterator first,iterator last)
{
	iterator i=copy(last,finish,first);
	destroy(i,finish);
	finish=finish-(last-first);
	return first;
}

  

insert()

P125 book

Spare space is first determined, if more than or equal to the number of elements added, can be inserted.

at this time:

First, the number of elements is determined after the insertion point, if the insertion point is greater than the number of elements in the new number of elements, then:

After the first [finish-n, finish) of the elements to the finish (total n elements), the update Finish, then [position, old_finish-n) of the elements to the old_finish and after (co mn elements), so that position ~ old_finish total of m elements are moved finished, and the space vacated n elements. In yet [position, position + n) covering the new elements.

It is less than the number of new elements, then:

The first generated element is inserted into position after the finish and on the nm, a total of nm.

Then position ~ old_finish m elements were moved to a new tail, so that the space vacated m elements.

Rewriting the insert element postition ~ old_finish, a total of m, the n-m + m = n, insertion is completed.

 

If space is insufficient:

First decide space: len = old_size + max (old_size, n); // the new length or twice the expansion can be installed, if the expansion will have to fit exactly hold old_size + n

Then allocate space for the new element

And then copies the original element before the insertion point to the new space, and then fill the element insertion of n, then the original copy element after the insertion point to the new space.

Then destructor element original space and free space.

Finally, adjust the position of the three iterator to the new space

Guess you like

Origin www.cnblogs.com/lxy-xf/p/11455670.html