-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherase.html
More file actions
146 lines (128 loc) · 4.36 KB
/
erase.html
File metadata and controls
146 lines (128 loc) · 4.36 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/cppdeque/erase.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:36 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>erase</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">C++ Double-ended Queues</a> > <a href=
"erase.html">erase</a>
</div>
<div class="name-format">
erase
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <deque>
iterator erase( iterator loc );
iterator erase( iterator start, iterator end );
</pre>
<p>The erase() function either deletes the element at location
<em>loc</em>, or deletes the elements between <em>start</em> and
<em>end</em> (including <em>start</em> but not including
<em>end</em>). The return value is the element after the last element
erased.</p>
<p>The first version of erase (the version that deletes a single
element at location <em>loc</em>) runs in <a href=
"../complexity.html">constant time</a> for lists and <a href=
"../complexity.html">linear time</a> for vectors, dequeues, and
strings. The multiple-element version of erase always takes <a href=
"../complexity.html">linear time</a>.</p>
<p>For example:</p>
<pre class="prettyprint">
// Create a vector, load it with the first ten characters of the alphabet
vector<char> alphaVector;
for( int i=0; i < 10; i++ ) {
alphaVector.push_back( i + 65 );
}
int size = alphaVector.size();
vector<char>::iterator startIterator;
vector<char>::iterator tempIterator;
for( int i=0; i < size; i++ ) {
startIterator = alphaVector.begin();
alphaVector.erase( startIterator );
// Display the vector
for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) {
cout << *tempIterator;
}
cout << endl;
}
</pre>
<p>That code would display the following output:</p>
<pre class="prettyprint">
BCDEFGHIJ
CDEFGHIJ
DEFGHIJ
EFGHIJ
FGHIJ
GHIJ
HIJ
IJ
J
</pre>
<p>In the next example, erase() is called with two iterators to
delete a range of elements from a vector:</p>
<pre class="prettyprint">
// create a vector, load it with the first ten characters of the alphabet
vector<char> alphaVector;
for( int i=0; i < 10; i++ ) {
alphaVector.push_back( i + 65 );
}
// display the complete vector
for( int i = 0; i < alphaVector.size(); i++ ) {
cout << alphaVector[i];
}
cout << endl;
// use erase to remove all but the first two and last three elements
// of the vector
alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 );
// display the modified vector
for( int i = 0; i < alphaVector.size(); i++ ) {
cout << alphaVector[i];
}
cout << endl;
</pre>
<p>When run, the above code displays:</p>
<pre class="prettyprint">
ABCDEFGHIJ
ABHIJ
</pre>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="clear.html">clear</a><br>
<a href="insert.html">insert</a><br>
<a href="pop_back.html">pop_back</a><br>
<a href="pop_front.html">pop_front</a><br>
(C++ Lists) <a href="../cpplist/remove.html">remove</a><br>
(C++ Lists) <a href="../cpplist/remove_if.html">remove_if</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppdeque/erase.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:36 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>