19

为什么没有std::pair迭代器?

std::pair应该提供iteratorandconst_iterator以及begin()and end()-- 只为他们的两个成员。

我认为这会很有用,因为这样我们可以将它们传递给期望可迭代的模板函数,比如vectoror set

这有什么缺点吗?

4

4 回答 4

27

一个原因是一对中的两个元素可以是不同的类型。这不适合迭代器模型。

元组也是如此,拥有迭代器可能更具吸引力。

如果您需要一个固定长度的廉价同质容器,您可以使用std::array<T, n>.

于 2012-12-06T17:55:31.683 回答
2

我不认为有任何特别的缺点,除了它只适用于pair<T,T>,而不是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";
}

当然你也想要一个const_iterator,接下来你会希望它是随机访问的(这意味着更多的操作员)。

就像每个人都说的那样,但这并不是真正pair的目的。它只是不是一个容器。

于 2012-12-06T18:46:12.310 回答
2

的目的std::pair不是成为一个传统的容器,而是作为一个元组,允许将两个潜在的异构对象视为一个对象。

此外,因为您可以直接访问该对的两个部分,并且因为配对的类型可能不相同,所以迭代器没有意义。

于 2012-12-06T17:57:55.477 回答
0

我想出了这个解决方案。不是很“性感”,但应该可以:

#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
于 2016-12-02T13:14:08.993 回答