Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
iter.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <iterator>
35#include <type_traits>
36#include <utility>
37
38namespace console {
45 template <class Iter>
46 struct IteratorPair : public std::pair<Iter, Iter> {
52 IteratorPair(Iter beg, Iter end) : std::pair<Iter, Iter>(beg, end) {}
53
54 typedef Iter iterator;
55 using value_type =
56 typename std::iterator_traits<Iter>::value_type;
57 using reference =
58 typename std::iterator_traits<Iter>::reference;
59 using pointer =
60 typename std::iterator_traits<Iter>::pointer;
61
66 iterator begin() { return this->first; }
71 iterator end() { return this->second; }
72 };
73
80 template <class Iter>
81 IteratorPair<Iter> iterpair(Iter beg, Iter end) {
82 return {beg, end};
83 }
84
91 template <class It1, class It2>
93 It1 it1;
94 It2 it2;
95
96 public:
97 typedef std::pair<typename std::iterator_traits<It1>::value_type,
98 typename std::iterator_traits<It2>::value_type>
100 typedef void pointer;
101 typedef std::pair<typename std::iterator_traits<It1>::reference,
102 typename std::iterator_traits<It2>::reference>
104 typedef std::forward_iterator_tag iterator_category;
105 typedef std::ptrdiff_t difference_type;
106
112 TiedIterators(It1 i1, It2 i2) : it1(i1), it2(i2) {}
113
119 ++it1;
120 ++it2;
121 return *this;
122 }
123
127 reference operator*() const { return {*it1, *it2}; }
128
136 bool operator==(const TiedIterators &other) const {
137 return it1 == other.it1 || it2 == other.it2;
138 }
139
147 bool operator!=(const TiedIterators &other) const {
148 return !(*this == other);
149 }
150 };
151
168 template <class C1, class C2>
169 IteratorPair<TiedIterators<typename C1::iterator, typename C2::iterator>>
170 zip(C1 &c1, C2 &c2) {
171 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
172 }
173
180 template <class C1, class C2>
181 IteratorPair<
182 TiedIterators<typename C1::const_iterator, typename C2::const_iterator>>
183 zip(const C1 &c1, const C2 &c2) {
184 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
185 }
186
193 template <class C1, class C2>
194 IteratorPair<
195 TiedIterators<typename C1::const_iterator, typename C2::iterator>>
196 zip(const C1 &c1, C2 &c2) {
197 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
198 }
199
206 template <class C1, class C2>
207 IteratorPair<
208 TiedIterators<typename C1::iterator, typename C2::const_iterator>>
209 zip(C1 &c1, const C2 &c2) {
210 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
211 }
212}
bool operator!=(const TiedIterators &other) const
比较是否与另一迭代器不等。
Definition iter.h:147
std::pair< typename std::iterator_traits< It1 >::reference, typename std::iterator_traits< It2 >::reference > reference
类型别名
Definition iter.h:103
void pointer
类型别名(不实际使用)
Definition iter.h:100
std::forward_iterator_tag iterator_category
类型别名
Definition iter.h:104
reference operator*() const
对迭代器组解引用。
Definition iter.h:127
TiedIterators & operator++()
将两个迭代器均前进一步。
Definition iter.h:118
bool operator==(const TiedIterators &other) const
比较是否与另一迭代器相等。
Definition iter.h:136
std::pair< typename std::iterator_traits< It1 >::value_type, typename std::iterator_traits< It2 >::value_type > value_type
类型别名
Definition iter.h:99
It2 it2
迭代器B
Definition iter.h:94
It1 it1
迭代器A
Definition iter.h:93
std::ptrdiff_t difference_type
类型别名
Definition iter.h:105
TiedIterators(It1 i1, It2 i2)
接受迭代器组的构造函数。
Definition iter.h:112
本库所有组件所在的顶层命名空间。
IteratorPair< TiedIterators< typename C1::iterator, typename C2::iterator > > zip(C1 &c1, C2 &c2)
将两个容器“拉链”式地绑定在一起,以便同时迭代。
Definition iter.h:170
IteratorPair< Iter > iterpair(Iter beg, Iter end)
工厂函数,构建 IteratorPair。
Definition iter.h:81
存储迭代器对,可以直接范围 for。
Definition iter.h:46
iterator end()
返回超尾迭代器。
Definition iter.h:71
Iter iterator
迭代器类型别名。
Definition iter.h:54
typename std::iterator_traits< Iter >::pointer pointer
指针类型别名。
Definition iter.h:59
iterator begin()
返回首迭代器。
Definition iter.h:66
typename std::iterator_traits< Iter >::value_type value_type
值类型别名。
Definition iter.h:55
IteratorPair(Iter beg, Iter end)
接受迭代器对的构造函数。
Definition iter.h:52
typename std::iterator_traits< Iter >::reference reference
引用类型别名。
Definition iter.h:57