# operator+(std::counted_iterator)

> C++ Atlas: `cppatlas.com/w/cpp/iterator/counted_iterator/operator+` | Source: https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator+ | Domain: Iterator | Standards: C++20

Returns an iterator adaptor which is advanced by n. The behavior is undefined if n is greater than the length recorded within x (i.e. if x + n result in undefined behavior).

## Official Specification Summary (cppreference)

```cpp
friend constexpr counted_iterator operator+(
    std::iter_difference_t<I> n, const counted_iterator& x )
        requires std::random_access_iterator<I>; // (since C++20)
```

Returns an iterator adaptor which is advanced by `n`. The behavior is undefined if `n` is greater than the length recorded within `x` (i.e. if `x + n` result in undefined behavior).

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::counted_iterator\<I> is an associated class of the arguments.

### Parameters

- `n` - the number of positions to increment the iterator
- `x` - the iterator adaptor to increment

### Return value

An iterator adaptor equal to `x + n`.

### Example

```cpp
#include <iostream>
#include <iterator>
#include <list>
#include <vector>

int main()
{
    std::vector v{0, 1, 2, 3, 4, 5};
    std::counted_iterator<std::vector<int>::iterator> p{v.begin() + 1, 4};
    std::cout << "*p:" << *p << ", count:" << p.count() << '\n';
    std::counted_iterator<std::vector<int>::iterator> q{2 + p};
    std::cout << "*q:" << *q << ", count:" << q.count() << '\n';

    std::list l{6, 7, 8, 9};
    std::counted_iterator<std::list<int>::iterator> r{l.begin(), 3};
    std::cout << "*r:" << *r << ", count:" << r.count() << '\n';
//  auto s{2 + r}; // error: the underlying iterator does
                   // not model std::random_access_iterator
}
```

Output:

```text
*p:1, count:4
*q:3, count:2
*r:6, count:3
```

### See also

- [`operator++`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith) (C++20), [`operator++(int)`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator+=`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator+`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator--`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator--(int)`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator-=`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith), [`operator-`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator_arith): advances or decrements the iterator (public member function)
- [`operator-`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator-) (C++20): computes the distance between two iterator adaptors (function template)
- [`operator-(std::default_sentinel_t)`](https://en.cppreference.com/w/cpp/iterator/counted_iterator/operator-2) (C++20): computes the signed distance to the end (function template)

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