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