Skip to content

vector_pro::emplace

C++
size_type emplace(size_type position, const value_type& val);
iterator_pro<value_type> emplace(iterator_pro<value_type> position, const value_type& val);
const_iterator_pro<value_type> emplace(const_iterator_pro<value_type> position, const value_type& val);

Construct and insert element

The container is extended by inserting a new element val at position.

This effectively increases the container size by one.

An automatic reallocation of the allocated storage space happens if -and only if- the new vector size surpasses the current vector capacity.

A similar member function exists, vector_pro::insert , which either copies or moves existing objects into the container.

Please note that if the variable position exceeds the bounds of the vector, this function will throw an exception .

Parameters

  1. position
    Position in the container where the new element is inserted.
    Can be size_type / iterator_pro / const_iterator_pro.
  2. val
    Value of the new element.

Return value

An position that points to the newly emplaced element.

Type will be same as the position. ( size_type / iterator_pro / const_iterator_pro )

Example

C++
// vector_pro::emplace
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * myvector contains: 10 200 100 20 30 300
 */

int main ()
{
  vector_pro<int> myvector = {10,20,30};

  auto it = myvector.emplace ( myvector.begin()+1, 100 );
  myvector.emplace ( it, 200 );
  myvector.emplace ( myvector.end(), 300 );

  std::cout << "myvector contains:";
  for (auto& x: myvector)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Complexity

Linear on the number of elements after position (moving).