-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorPair.cpp
More file actions
63 lines (51 loc) · 1.62 KB
/
IteratorPair.cpp
File metadata and controls
63 lines (51 loc) · 1.62 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* \file IteratorPair.cpp
* \brief Specify a range of data values without worrying about the underlying data structure
*
* \warning Iterators should expose a fixed, minimal interface such as a **pre-increment operator**
* \see https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Iterator_Pair
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
template <class T>
class Vector
/// Iterator-pair class
{
public:
template <class InputIterator>
Vector(InputIterator begin, InputIterator end)
{
_mem = new T[ std::distance(begin, end) ];
for (int i = 0; begin != end; ++ i) {
_mem[i] = *begin;
++ begin;
}
}
~Vector()
{
delete [] _mem; _mem = {};
}
private:
T *_mem {};
};
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
std::list<int> l(4);
std::cout << STD_TRACE_VAR(l) << std::endl;
// fill up list using iterator pair technique
std::fill(l.begin(), l.end(), 10);
std::cout << STD_TRACE_VAR(l) << std::endl;
// create vector using iterator pair technique
std::vector<int> v(l.cbegin(), l.cend());
std::cout << STD_TRACE_VAR(v) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
l: std::list (size=4): {0,0,0,0}
l: std::list (size=4): {10,10,10,10}
v: std::vector (size=4): {10,10,10,10}
#endif