forked from heavy3/programming-abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (62 loc) · 1.96 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (62 loc) · 1.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// main.cpp
//
// Implement the function:
//
// template <typename IterType>
// IterType max_element(IterType start, IterType end);
//
// from the <algorithm> library, which returns an iterator pointing to the
// largest element in the specified iterator range.
//
// --------------------------------------------------------------------------
// Attribution: "Programming Abstractions in C++" by Eric Roberts
// Chapter 20, Exercise 08
// Stanford University, Autumn Quarter 2012
// http://web.stanford.edu/class/archive/cs/cs106b/cs106b.1136/materials/CS106BX-Reader.pdf
// --------------------------------------------------------------------------
//
// Created by Glenn Streiff on 4/4/17
// Copyright © 2017 Glenn Streiff. All rights reserved.
//
#include <iostream>
#include "error.h"
#include "vector.h"
using namespace std;
const std::string HEADER = "CS106B Programming Abstractions in C++: Ex 20.08\n";
const std::string DETAIL = "STL Programming: max_element()";
const std::string BANNER = HEADER + DETAIL;
typedef Vector<int> CollType;
typedef CollType::iterator IterType;
// Function prototypes
IterType max_element(IterType start, IterType end);
// Main program
int main() {
cout << BANNER << endl << endl;
CollType v;
v += 8;
v += 6;
v += 7;
v += 5;
v += 3;
v += 0;
v += 9;
v += 8;
IterType it = max_element(v.begin(), v.end());
cout << "Max element = " << *it << " from v = " << v << endl;
return 0;
}
// Function: max_element
// Usage: IterType it = max_element(coll.begin(), coll.end());
// -----------------------------------------------------------
// Returns an iterator pointing to the maximum element within the bounds
// of a begin and end iterator for a collection.
IterType max_element(IterType start, IterType end) {
IterType maxSoFar = start;
for (IterType it = start; it < end; it++) {
if (*it > *maxSoFar) {
maxSoFar = it;
}
}
return maxSoFar;
}