[英]Why doesn't std::pair have iterators?
為什么std::pair
沒有迭代器?
std::pair
應該提供iterator
和const_iterator
以及begin()
和end()
- 只是為了他們的兩個成員。
我認為這將是有用的,因為我們可以將它們傳遞給期望迭代的模板化函數,如vector
或set
。
這有什么缺點嗎?
一個原因是一對中的兩個元素可以是不同類型的。 這不適合迭代器模型。
元組也是如此,其中迭代器可能更具吸引力。
如果你需要一個固定長度的廉價同質容器,你可以使用std::array<T, n>
。
std::pair
的目的不是作為傳統容器,而是作為一個元組,允許將兩個可能異構的對象視為單個對象。
另外,因為您可以直接訪問該對的兩個部分,並且因為配對的類型可能不相同,所以迭代器沒有任何意義。
除了它只適用於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
。 它不是一個容器。
我想出了這個解決方案。 不是很“性感”,但它應該工作:
#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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.