Console Library 4.7.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 <utility>
36#include <type_traits>
37
38namespace console
39{
46 template <class Iter>
47 struct IteratorPair : public std::pair<Iter, Iter>
48 {
54 IteratorPair(Iter beg, Iter end) : std::pair<Iter, Iter>(beg, end) {}
55 typedef Iter iterator;
60 iterator begin() { return this->first; }
65 iterator end() { return this->second; }
66 };
67
74 template <class Iter>
75 IteratorPair<Iter> iterpair(Iter beg, Iter end) { return {beg, end}; }
76
83 template <class It1, class It2>
85 {
86 It1 it1;
87 It2 it2;
88
89 public:
90 typedef std::pair<typename It1::value_type,
91 typename It2::value_type>
93 typedef void pointer;
94 typedef std::pair<typename It1::reference,
95 typename It2::reference>
97 typedef std::forward_iterator_tag iterator_category;
98 typedef std::ptrdiff_t difference_type;
99
105 TiedIterators(It1 i1, It2 i2) : it1(i1), it2(i2) {}
106
112 {
113 ++it1;
114 ++it2;
115 return *this;
116 }
117
122 {
123 return {*it1, *it2};
124 }
125
133 bool operator==(const TiedIterators &other) const
134 {
135 return it1 == other.it1 || it2 == other.it2;
136 }
137
145 bool operator!=(const TiedIterators &other) const
146 {
147 return !(*this == other);
148 }
149 };
150
166 template <class C1, class C2>
167 IteratorPair<TiedIterators<typename C1::iterator, typename C2::iterator>>
168 zip(C1 &c1, C2 &c2)
169 {
170 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
171 }
172
179 template <class C1, class C2>
180 IteratorPair<TiedIterators<typename C1::const_iterator, typename C2::const_iterator>>
181 zip(const C1 &c1, const C2 &c2)
182 {
183 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
184 }
185
192 template <class C1, class C2>
193 IteratorPair<TiedIterators<typename C1::const_iterator, typename C2::iterator>>
194 zip(const C1 &c1, C2 &c2)
195 {
196 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
197 }
198
205 template <class C1, class C2>
206 IteratorPair<TiedIterators<typename C1::iterator, typename C2::const_iterator>>
207 zip(C1 &c1, const C2 &c2)
208 {
209 return {{std::begin(c1), std::begin(c2)}, {std::end(c1), std::end(c2)}};
210 }
211}
bool operator!=(const TiedIterators &other) const
比较是否与另一迭代器不等。
Definition iter.h:145
void pointer
类型别名(不实际使用)
Definition iter.h:93
std::forward_iterator_tag iterator_category
类型别名
Definition iter.h:97
reference operator*() const
对迭代器组解引用。
Definition iter.h:121
std::pair< typename It1::value_type, typename It2::value_type > value_type
类型别名
Definition iter.h:92
TiedIterators & operator++()
将两个迭代器均前进一步。
Definition iter.h:111
bool operator==(const TiedIterators &other) const
比较是否与另一迭代器相等。
Definition iter.h:133
std::ptrdiff_t difference_type
类型别名
Definition iter.h:98
TiedIterators(It1 i1, It2 i2)
接受迭代器组的构造函数。
Definition iter.h:105
std::pair< typename It1::reference, typename It2::reference > reference
类型别名
Definition iter.h:96
本库所有组件所在的顶层命名空间。
IteratorPair< TiedIterators< typename C1::iterator, typename C2::iterator > > zip(C1 &c1, C2 &c2)
将两个容器“拉链”式地绑定在一起,以便同时迭代。
Definition iter.h:168
IteratorPair< Iter > iterpair(Iter beg, Iter end)
工厂函数,构建 IteratorPair。
Definition iter.h:75
存储迭代器对,可以直接范围 for。
Definition iter.h:48
iterator end()
返回超尾迭代器。
Definition iter.h:65
Iter iterator
迭代器类型别名。
Definition iter.h:55
iterator begin()
返回首迭代器。
Definition iter.h:60
IteratorPair(Iter beg, Iter end)
接受迭代器对的构造函数。
Definition iter.h:54