forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_function.cpp
109 lines (89 loc) · 3.88 KB
/
count_function.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
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
/*******************************************************************************
*
* Program: count() Standard Library Function Examples
*
* Description: Examples of using the count() standard libary function in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=VaQDB3kCqgU
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
// count() is defined inside of algorithm so we include the library
#include <algorithm>
#include <vector>
using namespace std;
// A very simple class for geometric squares with a side length
class Square
{
public:
// Square side length
int side;
// Constructor to set square side length
Square(int side) : side(side) {}
// Overloaded == equality operator, squares are equal if they have the
// same side length as determined by comparing the side lengths
bool operator==(const Square &squareA)
{
return (side == squareA.side);
}
};
int main()
{
// An array of ints with 3 occurrences of the value '2'
// ,X
int array[] = {1,2,3,4,5,1,2,1,2,3};
// Call count() to count the occurrences of the value 2 in the array. We
// pass array and "array+10" to specify the range in which to count the
// occurrences of 2. Technically array is a pointer to the first element in
// the array and array+10 is a pointer to an element 10 elements ahead of
// this first element (i.e. one past the actual last element in the array
// where X is above). The count function will count the occurrences of the
// value 2 in the range:
//
// [start, end)
//
// In other words, it will count up until but not included the end that
// we specify. The start and end arguments that specify the range are
// iterators, but in C++ pointers are iterators.
//
int count2 = count(array, array+10, 2);
// Output the count of occurrences of the value 2 in the array
cout << "2s: " << count2 << endl;
// Declare and initialize a vector to store int values with 1 occurrence of
// the value 1.
//
// ,X
vector<int> test {3,4,5,6,7,1,2,3,4,5};
// Call count() to count the occurrences of the value 1 in the vector test.
// The .begin() and .end() vector member functions return an iterator to the
// first element in the vector, and an iterator to the 'hypothetical' element
// one past the last element in the vector (visualized with X above). And so
// count() will count the occurrences of 1 that occur between the first and
// last elements in the vector.
//
int count1 = count(test.begin(), test.end(), 1);
// Output the count of 1s
cout << "1s: " << count1 << endl;
// Create a vector of square objects, with two squares having a side length
// of 3
vector<Square> squares { Square(2), Square(3), Square(3), Square(1) };
// Create a square object with a side length of 3
Square tocount = Square(3);
// Technically the count() function returns the number of elements in the
// range that "compare equally" to the 3rd argument. What that means is that
// it returns the number of elements that when compared using the equality
// operator == with the 3rd argument result in the value true. In the
// Square class we have overloaded the equality operator so that Squares are
// considered equal if they have the same side lengths. As a result when
// comparing the Square tocount to the Squares in our vector, we expect that
// the two Squares with side lengths of 3 in the vector will compare equally
// with to count (which has a side length of 3), and therefore we will have
// a count return value of 2.
//
int count3 = count(squares.begin(), squares.end(), tocount);
// Output the number of squares with a side length of 3
cout << "Squares with side==3 -> " << count3 << endl;
return 0;
}