-
Notifications
You must be signed in to change notification settings - Fork 3
/
counted_output_iterator.cpp
46 lines (39 loc) · 1.11 KB
/
counted_output_iterator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// An iterator that writes a fixed number of things, then stops.
#include <iterator>
template <typename Iterator>
class counted_output_iterator {
private:
Iterator base_;
size_t space_;
public:
// typedefs
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
typedef std::output_iterator_tag iterator_category;
counted_output_iterator(Iterator b, size_t sz) : base_(b), space_(sz) {}
counted_output_iterator& operator++() { return *this; }
counted_output_iterator& operator++(int) { return *this; }
counted_output_iterator& operator*() { return *this; }
template <typename T>
counted_output_iterator& operator=(const T &value)
{
if (space_ > 0)
{
--space_;
*base_++ = value;
}
return *this;
}
};
#include <algorithm>
#include <cassert>
int main () {
char dest[11] = {0};
assert(dest[10] == 0);
std::copy_n("abcdefghijklmnop", 12, counted_output_iterator<char *>(dest, 10));
for (int i = 0; i < 10; ++i)
assert(dest[i] == 'a' + i);
assert(dest[10] == 0);
}