-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch14_Sec8_int.cpp
63 lines (55 loc) · 1.4 KB
/
Ch14_Sec8_int.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
// C++ Primer 4th Edition Chapter 14 Exercises Section 14.8.1 Excercise 14.33 & 14.34
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
class GT
{
public:
GT ( int val = 0 ): bound(val) {}
bool operator() ( const int &val ) { return val > bound; }
private:
int bound;
};
class EQ
{
public:
EQ ( int val = 0 ): bound(val) {}
bool operator() ( const int &val ) { return val == bound; }
private:
int bound;
};
int main()
{
int val(0),newVal(0);
vector<int> ivec;
srand( time(NULL) );
for( int i = 0; i != 10; i++) {
val = rand() %20 + 1;
ivec.push_back( val );
cout << val<< "\t";
}
cout << endl;
//
cout << "The threshold value you want to use:" << endl;
cin >> val;
vector<int>::iterator iter = find_if( ivec.begin(), ivec.end(), GT(val) );
if( iter != ivec.end() ) {
cout << "The first element greater than " << val << " is: " << *iter << endl;
}
else {
cout << "No element is greater than " << val << endl;
}
cin.clear();
//
cout << "The specific value you want to replace, and the new value:" << endl;
cin >> val >> newVal;
replace_if( ivec.begin(), ivec.end(), EQ(val), newVal );
for( iter = ivec.begin(); iter != ivec.end(); iter++ ) {
cout << *iter << "\t";
}
cout << endl;
return 0;
}