# std::inplace_vector<T,N>::insert_range

> C++ Atlas: `cppatlas.com/w/cpp/container/inplace_vector/insert_range` | Source: https://en.cppreference.com/w/cpp/container/inplace_vector/insert_range | Domain: Containers | Standards: C++26

Inserts, in non-reversing order, copies of elements in rg before pos.

## Official Specification Summary (cppreference)

```cpp
template< container-compatible-range<T> R >
constexpr iterator insert_range( const_iterator pos, R&& rg ); // (since C++26)
```

Inserts, in non-reversing order, copies of elements in `rg` before `pos`.

Each iterator in the range `rg` is dereferenced exactly once.

`rg` must not overlap with the container. Otherwise, the behavior is undefined.

### Parameters

- `pos` - iterator before which the content will be inserted (`pos` may be the `end()` iterator)
- `rg` - a container compatible range, that is, an `input_range` whose elements are convertible to `T`

**Type requirements**

- `T` must be *EmplaceConstructible* into `inplace_vector` from `*ranges::begin(rg)`. Otherwise, the behavior is undefined.

### Return value

An `iterator` that points at the copy of the first element inserted into `inplace_vector` or at `pos` if `rg` is empty.

### Exceptions

- `std::bad_alloc`, if `ranges::distance(rg) + size() > capacity()`. The elements of `*this` are not modified.
- Any exception thrown by insertion (i.e. by copy/move constructor, move/copy assignment operator of `T`) or by any *LegacyInputIterator* operation. The elements of `*this` in the range `[​0​,` `pos)` are not modified.

### Example

```cpp
#include <cassert>
#include <inplace_vector>
#include <iterator>
#include <new>
#include <print>

int main()
{
    auto v = std::inplace_vector<int, 8>{0, 1, 2, 3};
    auto pos = std::next(v.begin(), 2);
    assert(*pos == 2);
    const auto rg = {-1, -2, -3};
    v.insert_range(pos, rg);
    std::println("{}", v);

    try
    {
        assert(v.size() + rg.size() > v.capacity());
        v.insert_range(pos, rg); // throws: no space
    }
    catch(const std::bad_alloc& ex)
    {
        std::println("{}", ex.what());
    }
}
```

Possible output:

```text
[0, 1, -1, -2, -3, 2, 3]
std::bad_alloc
```

### See also

- [`insert`](https://en.cppreference.com/w/cpp/container/inplace_vector/insert): inserts elements (public member function)
- [`append_range`](https://en.cppreference.com/w/cpp/container/inplace_vector/append_range): adds a range of elements to the end (public member function)
- [`try_append_range`](https://en.cppreference.com/w/cpp/container/inplace_vector/try_append_range): tries to add a range of elements to the end (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/container/inplace_vector/insert_range (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.