insert

C++ Library  
 

Header

<deque>
iterator insert(iterator it, const T& x = T()) 

Inserts object x at the position specified by iterator it, into the deque container. This version of the member function insert returns an iterator that points to the position where the new element was inserted into the deque.

void insert(iterator it, size_type n, const T& x)

Inserts n copies of object x starting at position specified by iterator it, into the deque container.

void insert(iterator it, const_iterator first, const_iterator last)

Inserts the objects specified by the range [first, last), starting at position specified by iterator it, into the deque container.

Any insertion operation can be expensive. To perform an insertion in the middle of a container, memory must be allocated at the start and end of the container. Then a bulk copy is performed to open space in the middle, at the location pointed to by it argument. Finally, the data is copied into the new location. The function becomes more costly as the size of the container increases.

Inserting an element invalidates all iterators and references.


Sample

#include <deque>
#include <iostream>

int main()
{
	//default constructor
	std::deque<int> c1 ;

	//create deque with 10 copies of 4
	std::deque<int> c2(10, 4) ;

	//copy constructor
	std::deque<int> c3(c2) ;

	int ai[] = {0, 1, 2, 3, 4, 5} ;
	int i ;
	std::deque<int> c4 ;

	//get_allocator
	std::deque<int>::allocator_type a1 = c4.get_allocator() ;

	//push_back
	for(i = 0; i < 5; i++)
		c4.push_back(ai[i]) ;

	//range copy constructor
	std::deque<int> c5(c4.begin(), c4.end()) ;

	//begin, end
	std::cout << "c4 (using begin, end) = " ;
	std::deque<int>::iterator Iter ;
	for(Iter = c4.begin(); Iter != c4.end(); Iter++)
		std::cout << *Iter << ", " ;
	std::cout << std::endl ;

	//rbegin, rend
	std::cout << "c4 (using rbegin, rend) = " ;
	std::deque<int>::reverse_iterator RevIter ;
	for(RevIter = c4.rbegin(); RevIter != c4.rend(); RevIter++)
		std::cout << *RevIter << ", " ;
	std::cout << std::endl ;

	//assign
	c2.assign(c5.begin(), c5.begin() + 3) ;

	c1.assign(10, 4) ;

	//at
	std::cout << "third element in c1 = " << c1.at(3) << std::endl ;

	//operator[]
	std::cout << "c4[3] = " << c4[3] << std::endl ;

	//back
	std::cout << "last element in c2 = " << c2.back() << std::endl ;

	//front
	std::cout << "first element in c2 = " << c2.front() << std::endl ;

	//size
	std::cout << "number of elements in c2 = " << c2.size() << std::endl ;

	//max_size
	std::cout << "max number of elements c2 can hold using current allocator = " 
		<< c2.max_size() << std::endl ;

	//erase
	c3.erase(c3.begin(), c3.begin() + 4) ;

	//clear
	c2.clear() ;

	//empty
	if (c2.empty() == true)
		std::cout << "c2 is now empty" << std::endl ;

	//resize
	c2.resize(10, 30) ;
	std::cout << "number of elements in c2 = " << c2.size() << std::endl ;
	std::cout << "last element in c2 = " << c2.back() << std::endl ;
	std::cout << "first element in c2 = " << c2.front() << std::endl ;

	//push_front
	c2.push_front(25) ;
	std::cout << "first element in c2 = " << c2.front() << std::endl ;

	//pop_back
	c2.pop_back() ;
	std::cout << "last element in c2 = " << c2.back() << std::endl ;

	//pop_front
	c2.pop_front() ;
	std::cout << "first element in c2 = " << c2.front() << std::endl ;

	//swap
	c3.swap(c2) ;
	std::cout << "number of elements in c3 = " << c3.size() << std::endl ;
	std::cout << "last element in c3 = " << c3.back() << std::endl ;
	std::cout << "first element in c3 = " << c3.front() << std::endl ;

	//insert
	c1.insert(c1.begin(), 20) ;
	c1.insert(c1.begin()+1, 4, 10) ;
	c1.insert(c1.begin()+2, c5.begin(), c5.end()) ;
	std::cout << "c1 = " ;
	for(Iter = c1.begin(); Iter != c1.end(); Iter++)
		std::cout << *Iter << ", " ;
	std::cout << std::endl ;

	return 0 ;
}

Program Output

c4 (using begin, end) = 0, 1, 2, 3, 4,
c4 (using rbegin, rend) = 4, 3, 2, 1, 0,
third element in c1 = 4
c4[3] = 3
last element in c2 = 2
first element in c2 = 0
number of elements in c2 = 3
max number of elements c2 can hold using current allocator = 1073741823
c2 is now empty
number of elements in c2 = 10
last element in c2 = 30
first element in c2 = 30
first element in c2 = 25
last element in c2 = 30
first element in c2 = 30
number of elements in c3 = 9
last element in c3 = 30
first element in c3 = 30
c1 = 20, 10, 0, 1, 2, 3, 4, 10, 10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.