public member function
<deque>
std::deque::insert single element (1)iterator insert (iterator position, const value_type& val);fill (2)
void insert (iterator position, size_type n, const value_type& val);range (3)
template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last);single element (1)
iterator insert (const_iterator position, const value_type& val);fill (2)
iterator insert (const_iterator position, size_type n, const value_type& val);range (3)
template <class InputIterator>iterator insert (const_iterator position, InputIterator first, InputIterator last);move (4)
iterator insert (const_iterator position, value_type&& val);initializer list (5)
iterator insert (const_iterator position, initializer_list<value_type> il);
Insert elements
The deque container is extended by inserting new elements before the element at the specified position.This effectively increases the container size by the amount of elements inserted.
Double-ended queues are designed to be efficient performing insertions (and removals) from either the end or the beginning of the sequence. Insertions on other positions are usually less efficient than in list or forward_list containers.
The parameters determine how many elements are inserted and to which values they are initialized:
Member type iterator is a random access iterator type that points to elements.
The storage for the new elements is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// inserting into a deque
#include <iostream>
#include <deque>
#include <vector>
int main ()
{
std::deque<int> mydeque;
// set some initial values:
for (int i=1; i<6; i++) mydeque.push_back(i); // 1 2 3 4 5
std::deque<int>::iterator it = mydeque.begin();
++it;
it = mydeque.insert (it,10); // 1 10 2 3 4 5
// "it" now points to the newly inserted 10
mydeque.insert (it,2,20); // 1 20 20 10 2 3 4 5
// "it" no longer valid!
it = mydeque.begin()+2;
std::vector<int> myvector (2,30);
mydeque.insert (it,myvector.begin(),myvector.end());
// 1 20 30 30 20 10 2 3 4 5
std::cout << "mydeque contains:";
for (it=mydeque.begin(); it!=mydeque.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output:
mydeque contains: 1 20 30 30 20 10 2 3 4 5
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4