-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise 5
112 lines (85 loc) · 2.83 KB
/
Exercise 5
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 21 - Exercise 5
What would we have to do if we couldn't return end() to indicate "not found"?
Redesign and re-implement find() and count() to take iterators to the first
and last elements. Compare the results to the standard versions.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
#include <unordered_map>
#include <numeric>
//--GLOBALS--//
// -----------------------------------------------------------------------------
template<typename T, typename It>
typename iterator_traits<It>::difference_type myCount(It first, It last, const T& value)
{
typename iterator_traits<It>::difference_type count = 0;
for (; first != last; ++first)
if (*first == value)
++count;
// check last - this will error if end() is passed in
if (*first == value)
++count;
return count;
}
// -----------------------------------------------------------------------------
template<typename T, typename It>
bool myFind(It first, It last, const T& value, It& output)
{
for (; first != last; ++first)
{
if (*first == value)
{
output = first;
return true;
}
}
// check last - this will error if end() is passed in
if (*first == value)
{
output = first;
return true;
}
return false;
}
// -----------------------------------------------------------------------------
void fct()
{
const string fromFile = "CorporateGibberish.txt"; // found in exercise 1 folder
const string toFile = "AboutUs.txt";
ifstream is{ fromFile };
ofstream os{ toFile };
istream_iterator<string> ii{ is };
istream_iterator<string> eos;
ostream_iterator<string> oo{ os, "\n" };
vector<string> b2{ ii, eos };
copy(b2.begin(), b2.end(), oo);
int num = myCount(b2.begin(), std::prev(b2.end()), "and");
cout << "Number of times 'and' was seen: " << num << endl;
vector<string>::iterator it;
if(myFind(b2.begin(), std::prev(b2.end()), "and", it))
cout << "First 'and' is at position: " << it - b2.begin() << endl;
else
cout << "Not found." << endl;
if (myFind(b2.begin(), std::prev(b2.end()), "E", it))
cout << "First 'E' is at position: " << it - b2.begin() << endl;
else
cout << "Not found." << endl;
}
// -----------------------------------------------------------------------------
int main()
{
// done to keep an eye on memory tracking (it will always report as a leak if done in main)
fct();
keep_window_open();
_CrtDumpMemoryLeaks();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------