C++ std::vector emplace_back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

In C++, std::vector::emplace_back() builds a new element at the back of the vector. You give it the constructor arguments, and the vector creates the object in place.

It was added in C++11. It is often used instead of push_back() when the element type needs several arguments to be built. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Pass the constructor arguments of the element.

C++
v.emplace_back(5);
names.emplace_back(3, 'a');     // std::string(3, 'a')
auto& ref = v.emplace_back(7);  // C++17
CallWhat it does
v.emplace_back(5)Builds a new element 5 after the current last element.
names.emplace_back(3, 'a')Calls the string constructor with (3, 'a') and stores the string "aaa".
auto& ref = v.emplace_back(7)Since C++17 the function returns a reference to the new element.

The matching front version is emplace_front(). The classic copy version is push_back().

Library declarations
C++
template<class... Args>
void      emplace_back(Args&&... args);      // C++11

template<class... Args>
reference emplace_back(Args&&... args);      // C++17

Parameters

ParameterDescription
args...Required. The arguments passed to the constructor of the element type T.

Return Value

Return ValueDescription
Reference (C++17)A reference to the new element. In C++11 and C++14 the function returns void.

C++ Examples

Example 1 — emplace_back() with Numbers

For simple types it works like push_back().

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2};

    v.emplace_back(3);
    v.emplace_back(4);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector starts as 1 2.
  2. Each emplace_back() builds one new element at the back end.
  3. The final order is 1 2 3 4, because later calls go further to the back.

Example 2 — emplace_back() with String Arguments

Pass the arguments of the std::string constructor instead of a ready string.

C++
#include <vector>
#include <iostream>
#include <string>

int main() {
    std::vector<std::string> names;

    names.emplace_back(3, 'a');
    names.emplace_back("Mia");

    for (const std::string& s : names) {
        std::cout << s << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. emplace_back(3, 'a') forwards 3 and 'a' to the string constructor, which makes "aaa".
  2. emplace_back("Mia") builds a string from the text directly inside the vector.
  3. push_back(3, 'a') would not compile, because push_back() needs one finished object.

Example 3 — Use the Reference from emplace_back()

Since C++17 you can change the new element right away.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;

    auto& ref = v.emplace_back(5);
    ref += 10;

    std::cout << "back: " << v.back() << '\n';
    return 0;
}

How It Works

  1. emplace_back(5) adds 5 and returns a reference to it. Compile with C++17 or newer.
  2. ref += 10 changes the element inside the vector, because ref is a reference and not a copy.
  3. back() now reads 15, which proves that the vector element was modified.

Frequently Asked Questions

It builds a new element at the back of the vector from the constructor arguments you pass. The element is created in place, so no extra copy is needed.
push_back() takes an object that already exists and copies or moves it. emplace_back() takes constructor arguments and builds the object inside the vector.
Since C++17 it returns a reference to the new element. In C++11 and C++14 it returns void.
Not always. For simple types such as int there is no difference. It helps most with bigger objects that have a costly copy.
Yes, with the same syntax. A vector can also add at the front with emplace_front(), which a vector cannot do.

Next: empty()

Check whether the vector has no elements with std::vector.

empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful