20

Why doesn't std::pair have iterators?

std::pair should provide iterator and const_iterator as well as begin() and end() -- just for their two members.

I think it would be useful because then we could pass them into templated functions that expect iterables, like vector or set.

Are there any downsides to this?

Frank
  • 64,140
  • 93
  • 237
  • 324
  • 10
    `std::pair`. Iterators only work on homogeneous containers. – R. Martinho Fernandes Dec 06 '12 at 17:54
  • Although `std::pair` is sometimes used as a range, nothing about it requires or even encourages use as such -- so specializing for that wouldn't be too great. An early draft of C++11 actually had this at one point. See http://stackoverflow.com/questions/6167598/why-was-pair-range-access-removed-from-c11 – Cory Nelson Dec 06 '12 at 17:57
  • 2
    If you find yourself wanting to pass your objects to such templated functions then you should probably use `std::array` rather than `std::pair`. – leftaroundabout Dec 06 '12 at 18:01

4 Answers4

30

One reason is that the two elements of a pair can be of different types. This doesn't fit with the iterator model.

The same goes for tuples, where having iterators would perhaps be even more appealing.

If you need an inexpensive homogenous container of a fixed length, you could use std::array<T, n>.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

The purpose of std::pair is not to be a traditional container but rather to serve as a tuple that allows two potentially heterogeneous objects to be treated as a single one.

Additionally, because you have direct access to both parts of the pair, and because the types paired may not be the same, an iterator makes no sense.

Omaha
  • 2,262
  • 15
  • 18
2

I don't think there's any particular downside other than that it only works for pair<T,T>, not pair<T,U>.

#include <utility>
#include <iterator>
#include <vector>
#include <iostream>

namespace itpair {
    template <typename T>
    struct pair_iterator : std::iterator<std::forward_iterator_tag, T> {
        std::pair<T,T> *container;
        int idx;
        pair_iterator(std::pair<T,T> *container, int idx) : container(container), idx(idx) {}
        T &operator*() const {
            return idx ? container->second : container->first;
        }
        T *operator->() const {
            return &*this;
        }
        friend pair_iterator &operator++(pair_iterator &self) {
            self.idx += 1;
            return self;
        }
        friend pair_iterator operator++(pair_iterator &self, int) {
            pair_iterator result = self;
            ++self;
            return result;
        }
        friend bool operator==(const pair_iterator &self, const pair_iterator &other) {
            return self.container == other.container && self.idx == other.idx;
        }
        friend bool operator!=(const pair_iterator &self, const pair_iterator &other) {
            return !(self == other);
        }
    };

    template <typename T>
    pair_iterator<T> begin(std::pair<T,T> &p) {
        return pair_iterator<T>(&p, 0);
    }
    template <typename T>
    pair_iterator<T> end(std::pair<T,T> &p) {
        return pair_iterator<T>(&p, 2);
    }
}

int main() {
    std::pair<int,int> p = std::make_pair(1, 2);
    using namespace itpair;
    std::vector<int> v(begin(p), end(p));
    std::cout << v[0] << " " << v[1] << "\n";
}

Of course you want a const_iterator too, and next you'll be wanting it to be random-access (which means more operators).

Like everyone says, though, that's not really what pair is for. It's just not a Container.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
0

I came up with this solution. Not very "sexy", but it should work:

#include <type_traits>
#include <iterator>
#include <utility>

#include <boost/optional.hpp>

namespace pair_iterator {

template <class A, class B, class Pair>
class PairIterator {
public:
    using iterator_category = std::random_access_iterator_tag;
    using value_type = std::common_type_t<A, B>;
    using difference_type = std::ptrdiff_t;
    using pointer = std::add_pointer_t<value_type>;
    using reference = std::add_lvalue_reference_t<value_type>;
    using const_reference = std::add_lvalue_reference_t<const value_type>;
private:
    boost::optional<Pair &> pair = {};
    difference_type index = 2;
public:
    PairIterator(
        const boost::optional<Pair &> &pair = {},
        difference_type index = 2
    ) : pair(pair), index(index) {}

    // Iterator

    PairIterator(PairIterator&&) = default;
    PairIterator(const PairIterator&) = default;
    PairIterator &operator =(PairIterator&&) = default;
    PairIterator &operator =(const PairIterator&) = default;
    ~PairIterator() = default;

    void swap(PairIterator &other) {
        std::swap(pair, other.pair);
        std::swap(index, other.index);
    }

    reference operator *() {
        return index == 0 ? pair->first : pair->second;
    }

    const_reference operator *() const {
        return index == 0 ? pair->first : pair->second;
    }

    PairIterator &operator ++() {
        ++index;
        return *this;
    }

    // InputIterator

    bool operator ==(const PairIterator &other) const {
        return index == other.index;
    }

    bool operator !=(const PairIterator &other) const {
        return index != other.index;
    }

    PairIterator operator ++(int) const {
        return { pair, index+1 };
    }

    // ForwardIterator

    // BidirectionalIterator

    PairIterator &operator --() {
        --index;
        return *this;
    }

    PairIterator operator --(int) const {
        return { pair, index-1 };
    }

    // RandomAccessIterator

    PairIterator &operator +=(difference_type n) {
        index += n;
        return *this;
    }

    PairIterator operator +(difference_type n) const {
        return { pair, index+n };
    }

    PairIterator &operator -=(difference_type n) {
        index -= n;
        return *this;
    }

    PairIterator operator -(difference_type n) const {
        return { pair, index-n };
    }

    difference_type operator -(const PairIterator &other) const {
        return index - other.index;
    }

    reference operator [](difference_type n) {
        return (index+n) == 0 ? pair->first : pair->second;
    }

    const_reference operator [](difference_type n) const {
        return (index+n) == 0 ? pair->first : pair->second;
    }

    bool operator <(const PairIterator &other) const {
        return index < other.index;
    }

    bool operator >(const PairIterator &other) const {
        return index > other.index;
    }

    bool operator <=(const PairIterator &other) const {
        return index <= other.index;
    }

    bool operator >=(const PairIterator &other) const {
        return index >= other.index;
    }
};

template <class A, class B>
auto begin(std::pair<A, B> &pair) ->
PairIterator<A, B, std::pair<A, B>> {
    return { pair, 0 };
}

template <class A, class B>
auto end(std::pair<A, B> &pair) ->
PairIterator<A, B, std::pair<A, B>> {
    return { pair, 2 };
}

template <class A, class B>
auto begin(const std::pair<A, B> &pair) ->
PairIterator<const A, const B, const std::pair<A, B>> {
    return { pair, 0 };
}

template <class A, class B>
auto end(const std::pair<A, B> &pair) ->
PairIterator<const A, const B, const std::pair<A, B>> {
    return { pair, 2 };
}

} // namespace pair_iterator

namespace std {

using pair_iterator::begin;
using pair_iterator::end;

} // namespace std
Kijewski
  • 25,517
  • 12
  • 101
  • 143