The Wayback Machine - https://web.archive.org/web/20240912102753/https://www.geeksforgeeks.org/vectoremplace_back-c-stl/
Open In App

vector::emplace_back in C++ STL

Last Updated : 06 Feb, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

vector::emplace_back()

This function is used to insert a new element into the vector container, the new element is added to the end of the vector.
Syntax :

vectorname.emplace_back(value)
Parameters :
The element to be inserted into the vector
is passed as the parameter.
Result :
The parameter is added to the
vector at the end position.

Examples:

Input: myvector{1, 2, 3, 4, 5};
         myvector.emplace_back(6);
Output: myvector = 1, 2, 3, 4, 5, 6

Input: myvector{};
         myvector.emplace_back(4);
Output: myvector = 4

Errors and Exceptions:

  1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
  2. The parameter should be of the same type as that of the container, otherwise, an error is thrown.

Example 1:





Output:

1 2 3 4 5 6

Example 2:





Output:

This is a computer science portal

Example 3:





Output:

a, c, x, y, z

Time Complexity: O(1)

Application:
Given an empty vector, add integers to it using emplace_back function and then calculate its size.

Input  : 1, 2, 3, 4, 5, 6
Output : 6

Algorithm

  1. Add elements to the vector using emplace_back function
  2. Check if the size of the vector is 0, if not, increment the counter variable initialised as 0, and pop the back element.
  3. Repeat this step until the size of the vector becomes 0.
  4. Print the final value of the variable.





Output:

6

emplace_back() vs push_back()

  1. push_back() copies a string into a vector. First, a new string object will be implicitly created initialized with provided char*. Then push_back will be called which will copy this string into the vector using the move constructor because the original string is a temporary object. Then the temporary object will be destroyed.
  2. emplace_back() constructs a string in-place, so no temporary string will be created but rather emplace_back() will be called directly with char* argument. It will then create a string to be stored in the vector initialized with this char*. So, in this case, we avoid constructing and destroying an unnecessary temporary string object.

Please see emplace vs insert in C++ STL for details.





Output:

a 24
b 25


Similar Reads

list::emplace_front() and list::emplace_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::emplace_front()This function is used to insert a new element into the list container, the new
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector<data_structure/type> vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator=This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin()begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector::push_back() and vector::pop_back() in C++ STL
STL vector container provides two functions: push_back() and pop_back() for insertion and deletion from the end of the vector. In this article, we will learn discuss about these methods. vector::push_back()The push_back() function is used to push or insert an element into a vector at the back. It means that the new value is inserted into the vector
3 min read
vector::crend() & vector::crbegin() with example
These functions return useful iterators to access vector elements in reverse order (from end to beginning) Using vector::crend() It's a public member function that returns a const_reverse_iterator pointing to the element preceding the first element. Return Value A const_reverse_iterator to the reverse end of the sequence. Syntax: const_reverse_iter
2 min read
How to flatten a Vector of Vectors or 2D Vector in C++
Given a Vector of Vectors (2D vector), the task is to flatten this 2d vector. Examples: Input: vector = [[1, 2, 3, 4], [5, 6], [7, 8]] Output: 1 2 3 4 5 6 7 8 Input: vector = [[1, 2], [3], [4, 5, 6, 8]] Output: 1 2 3 4 5 6 8 Algorithm: 2D Vector can be flattened using iterators.Store starting and ending iterator of every vector in two arrays, iStar
2 min read
std::vector::resize() vs. std::vector::reserve()
In C++, vectors are dynamically resizable arrays provided by STL. They can automatically change their size according to the number of elements and users also can change its size using the std::vector::resize()and std::vector::reserve(). But there is some difference in how these functions works. In this article, we will learn what are the difference
5 min read
Is std::vector or boost::vector Thread Safe?
In C++, a common question that arises is: Is std::vector or boost::vector thread-safe? The straightforward answer is no, neither std::vector nor boost::vector is thread-safe. In this article, we will learn why std::vector and boost::vector are not thread-safe and explore some practical alternatives for making them safe to use in a multi-threaded en
5 min read
vector rbegin() and rend() function in C++ STL
vector::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax: vector_name.rbegin() Parameters: The function does not accept any parameter.Return value: The function returns a reverse iterator pointing to the last element in the container.Program to demonstrate the vector::
2 min read
vector :: assign() in C++ STL
vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary. The syntax for assigning constant values: vectorname.assign(int size, int value) Parameters: size - number of values to be assigned value - value to be assigned to the vectorname Program 1
3 min read
Copy File To Vector in C++ STL
Prerequisite: Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the contents of a file into a vector using C++ STL. Approach
2 min read
vector data() function in C++ STL
The std::vector::data() is an STL in C++ which returns a direct pointer to the memory array used internally by the vector to store its owned elements. Syntax: vector_name.data() Parameters: The function does not accept any parameters.Return value: The function returns a pointer to the first element in the array which is used internally by the vecto
1 min read
vector emplace() function in C++ STL
The vector::emplace() is an STL in C++ which extends the container by inserting a new element at the position. Reallocation happens only if there is a need for more space. Here the container size increases by one.Syntax: template iterator vector_name.emplace (const_iterator position, element); Parameter: The function accepts two mandatory parameter
3 min read
vector max_size() function in C++ STL
The vector::max_size() is a built-in function in C++ STL which returns the maximum number of elements that can be held by the vector container. Syntax: vector_name.max_size() Parameters: The function does not accept any parameters. Return value: The function returns the maximum numbers that can fit into the vector container. Time Complexity - Const
1 min read
vector shrink_to_fit() function in C++ STL
The vector::shrink_to_fit() is a built-in function in C++ STL which reduces the capacity of the container to fit its size and destroys all elements beyond the capacity. Syntax: vector_name.shrink_to_fit() Parameters: The function does not accept any parameters. Return value: The function does not returns anything. Time Complexity - Linear O(N) Belo
2 min read
vector capacity() function in C++ STL
The vector::capacity() function is a built-in function which returns the size of the storage space currently allocated for the vector, expressed in terms of elements. This capacity is not necessarily equal to the vector size. It can be equal to or greater, with the extra space allowing to accommodate for growth without the need to reallocate on eac
2 min read
How to find common elements between two Vector using STL in C++?
Given two vectors, find common elements between these two vectors using STL in C++. Example: Input: vec1 = {1, 45, 54, 71, 76, 12}, vec2 = {1, 7, 5, 4, 6, 12} Output: {1, 12} Input: vec1 = {1, 7, 5, 4, 6, 12}, vec2 = {10, 12, 11} Output: {1, 4, 12} Approach: Common elements can be found with the help of set_intersection() function provided in STL.
2 min read
How to sort a Vector in descending order using STL in C++?
Given a vector, sort this vector in descending order using STL in C++. Example: Input: vec = {1, 45, 54, 71, 76, 12} Output: {76, 71, 54, 45, 12, 1} Input: vec = {1, 7, 5, 4, 6, 12} Output: {12, 7, 6, 5, 4, 1} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax 1: sort(arr, arr + n, greater<T>()); C/C++ Code
2 min read
How to find the sum of elements of a Vector using STL in C++?
Given a vector, find the sum of the elements of this vector using STL in C++. Example:Input: vec = {1, 45, 54, 71, 76, 12} Output: 259 Input: vec = {1, 7, 5, 4, 6, 12} Output: 35Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax:accumulate(first_index, last_index, initial value of sum); C/C++ Code // C++ progr
2 min read
How to find the maximum element of a Vector using STL in C++?
Given a vector, find the maximum element of this vector using STL in C++. Example: Input: {1, 45, 54, 71, 76, 12} Output: 76 Input: {1, 7, 5, 4, 6, 12} Output: 12 Approach: Max or Maximum element can be found with the help of *max_element() function provided in STL. Syntax: *max_element (first_index, last_index); C/C++ Code // C++ program to find t
1 min read
Vector of Vectors in C++ STL with Examples
Prerequisite: Vectors in C++ STL Vectors are known as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector of Vectors is a two-dimensional vector with a variable number of rows where each row is vector. Each index of vector stor
5 min read
Create a balanced BST using vector in C++ STL
Given an unsorted vector arr, the task is to create a balanced binary search tree using the elements of the array. Note: There can be more than one balanced BST. Forming any is acceptable Examples: Input: arr[] = { 2, 1, 3}Output: 2 1 3Explanation: The tree formed is show below. The preorder traversal is 2 1 3 2 / \ 1 3 Input: arr[] = {4, 3, 1, 2}O
5 min read
Difference Between Size and Capacity of a Vector in C++ STL
In this article, we are going to study the difference between the size and capacity of a vector in C++ STL. The size of the vector is defined as the total number of elements present in the vector. Vector is a dynamic array so it changes with the user input, unlike the static arrays which we define with the subscript operators "[]" which remain cons
3 min read
C++ STL - Vector in Reverse Order
Prerequisite: Vectors in C++ A vector can be printed in reverse order with the following methods: By traversing in the backward direction using indexingBy traversing in the backward direction using begin() and end() functions in C++ STLBy traversing in the backward direction using rbegin() and rend() functions in C++ STL Examples: Input: vector = {
3 min read
Article Tags :
Practice Tags :