![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Deque::push_back() Function
The C++ std::deque::push_back() function is used to insert the element to the end of the deque, increasing its size by one. This function ensures that the existing elements maintain their order, and any required memory reallocation is managed internally.
This function has 2 polymorphic variants: with using the default version and the move version (you can find the syntaxes of all the variants below).
Syntax
Following is the syntax for std::deque::push_back() function.
void push_back (const value_type& val); or void push_back (value_type&& val);
Parameters
- val − It indicates the value to be inserted to the deque.
Return value
It does not return anything.
Exceptions
This function never throws exception.
Time complexity
The time complexity of this function is constant i.e. O(1)
Example
In the following example, we are going to consider the basic usage of the push_back() function.
#include <iostream> #include <deque> int main() { std::deque<char> a; a.push_back('A'); a.push_back('B'); a.push_back('C'); for (auto x = a.begin(); x != a.end(); ++x) { std::cout << *x << " "; } std::cout << std::endl; return 0; }
Output
Output of the above code is as follows −
A B C
Example
Consider the another scenario, where we are going to use the push_back() function with strings.
#include <iostream> #include <deque> #include <string> int main() { std::deque<std::string> a; a.push_back("TP"); a.push_back("TutorialsPoint"); for (const auto& str : a) { std::cout << str << " "; } std::cout << std::endl; return 0; }
Output
Following is the output of the above code −
TP TutorialsPoint
Example
Let's look at the following example, where we are going to append the elements to the existing deque.
#include <iostream> #include <deque> int main() { std::deque<int> a = {01,12,23}; a.push_back(34); a.push_back(45); for (auto x = a.begin(); x != a.end(); ++x) { std::cout << *x << " "; } std::cout << std::endl; return 0; }
Output
If we run the above code it will generate the following output −
1 12 23 34 45