Returns the element with index idx (throws range-error exception if idx is out of range)
c.front()
Returns the first element (no check whether a first element exists)
c.back()
Returns the last element (no check whether a last element exists)
c.begin()
Returns a random-access iterator for the first element
c.end()
Returns a random-access iterator for the position after the last element
c.cbegin()
Returns a constant random-access iterator for the first element (since C++11)
c.cend()
Returns a constant random-access iterator for the position after the last element (since C++11)
c.rbegin()
Returns a reverse iterator for the first element of a reverse iteration
c.rend()
Returns a reverse iterator for the position after the last element of a reverse iteration
c.crbegin()
Returns a constant reverse iterator for the first element of a reverse iteration (since C++11)
c.crend()
Returns a constant reverse iterator for the position after the last element of a reverse iteration (since C++11)
Modifying Operations of Deque
Operation
Effect
c = c2
Assigns all elements of c2 to c
c = rv
Move assigns all elements of the rvalue rv to c (since C++11)
c = initlist
Assigns all elements of the initializer list initlist to c (since C++11)
c.assign(n, elem)
Assigns n copies of element elem
c.assign(beg, end)
Assigns the elements of the range [beg,end)
c.assign(initlist)
Assigns all the elements of the initializer list initlist
c1.swap(c2)
Swaps the data of c1 and c2
swap(c1, c2)
Swaps the data of c1 and c2
c.push_back(elem)
Appends a copy of elem at the end
c.pop_back()
Removes the last element (does not return it)
c.push_front(elem)
Inserts a copy of elem at the beginning
c.pop_front()
Removes the first element (does not return it)
c.insert(pos, elem)
Inserts a copy of elem before iterator position pos and returns the position of the new element
c.insert(pos, n, elem)
Inserts n copies of elem before iterator position pos and returns the position of the first new element (or pos if there is no new element)
c.insert(pos, beg, end)
Inserts a copy of all elements of the range [beg,end) before iterator position pos and returns the position of the first new element (or pos if there is no new element)
c.insert(pos, initlist)
Inserts a copy of all elements of the initializer list initlist before iterator position pos and returns the position of the first new element (or pos if there is no new element; since C++11)
c.emplace(pos, args…)
Inserts a copy of an element initialized with args before iterator position pos and returns the position of the new element (since C++11)
c.emplace_back(args…)
Appends a copy of an element initialized with args at the end (returns nothing; since C++11)
c.emplace_front(args…)
Inserts a copy of an element initialized with args at the beginning (returns nothing; since C++11)
c.erase(pos)
Removes the element at iterator position pos and returns the position of the next element
c.erase(beg, end)
Removes all elements of the range [beg,end) and returns the position of the next element
c.resize(num)
Changes the number of elements to num (if size() grows new elements are created by their default constructor)
c.resize(num, elem)
Changes the number of elements to num (if size() grows new elements are copies of elem)
// Examples of Using Deque #include<iostream> #include<deque> #include<string> #include<algorithm> #include<iterator> usingnamespacestd;
intmain() { // create empty deque of strings deque<string> coll; // insert several elements coll.assign(3, string("string")); coll.push_back("last string"); coll.push_front("first string"); // print elements separated by newlines copy(coll.cbegin(), coll.cend(), ostream_iterator<string>(cout, "\n")); cout<<endl; // remove first and last element coll.pop_front(); coll.pop_back(); // insert "another" into every element but the first for (unsigned i = 1; i < coll.size(); ++i) { coll[i] = "another " + coll[i]; } // change size to four elements coll.resize(4, "resized string"); // print elements separated by newlines copy(coll.cbegin(), coll.cend(), ostream_iterator<string>(cout, "\n")); system("Pause"); }