HOWTO · C++

How to Add Element to Vector of Pairs in C++

This article demonstrates how to add an element to a vector of pairs in C++

On this page

To append a two-value element to a C++ vector of pairs, store each element as std::pair<T1, T2> and add a suitable pair value with push_back, or pass its two constructor arguments to emplace_back. The pair’s component types can differ, such as an integer key and a string value.

Represent a vector of pairs

A declaration such as std::vector<std::pair<int, std::string>> describes a vector whose elements are pairs: each pair contains one int and one std::string. The two values belong to each element; they are not the number of elements in the vector. Include <vector>, <utility>, and <string> for these types and pair operations. The example below also uses <iostream> to print the result.

The initializer list starts the vector with two elements. Each nested brace group initializes one pair, so {12, "twelve"} is one element with an integer first value and a string second value. The vector’s size is therefore two before the append operations run. After an append, the new pair is at the end and the earlier elements remain in their original order.

Append a pair with push_back

push_back appends a pair value to the vector. std::make_pair is a convenient way to create that value from the supplied arguments, but it is not required: an explicitly typed std::pair<int, std::string> can also be passed. In either case, the value must be compatible with the vector’s element type. The example shows both forms, then prints each pair’s first and second members so you can see the appended elements.

The explicitly typed variable makes the destination component types clear: its first value is an int, and its second is a std::string. Passing that variable to push_back copies its pair value into the vector. If you need to preserve a pair variable for later use, this form keeps the source variable available after appending.

Run this example to compare the insertion forms. The supplied runtime execution completed successfully with exit code 0 and printed these five pairs in order:

#include <iostream>
#include <string>
#include <utility>
#include <vector>

int main() {
  std::vector<std::pair<int, std::string>> items = {
      {12, "twelve"}, {32, "thirty-two"}};

  items.push_back(std::make_pair(55, std::string("fifty-five")));
  std::pair<int, std::string> next{70, "seventy"};
  items.push_back(next);
  items.emplace_back(88, "eighty-eight");

  for (const auto& item : items) {
    std::cout << '(' << item.first << ", " << item.second << ")\n";
  }
}
(12, twelve)
(32, thirty-two)
(55, fifty-five)
(70, seventy)
(88, eighty-eight)

Construct a pair in place with emplace_back

emplace_back appends an element by forwarding its arguments to construct the vector’s pair element at the end. For std::pair<int, std::string>, passing an integer and a string-compatible value supplies the two pair constructor arguments. The element type must be constructible from the arguments you provide. In the shared example, emplace_back(88, "eighty-eight") constructs and appends the pair without first naming a separate pair variable.

Choose between push_back and emplace_back

Use push_back when you already have a pair value, whether it came from std::make_pair or an explicitly typed std::pair. Use emplace_back when you want to provide constructor arguments for the new pair directly. Both append one element to the vector; choose based on whether your code has a pair value or the values from which to construct one. This distinction describes how the element is supplied, not a guarantee that either operation is faster.