COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


STL Algorithms




(page 2)


find


template
It find(It begin, It end, T value);


(page 3)


find_if


template
It find_if(It begin, It end, Pred pred);


(page 4)


find_if example


bool is_negative(int i) {
return (i < 0);
}
int main() {
list l;
// fill values...
list::iterator i = find_if(l.begin(),
l.end(), is_negative);
if (i != l.end())
cout << "first negative is "
<< *i << endl;
}


(page 5)


Searching backwards


bool is_negative(int i) {
return (i < 0);
}
int main() {
list l;
// fill values...
list::reverse_iterator i =
find_if(l.rbegin(), l.rend(), is_negative);
if (i != l.rend())
cout << "last negative is "
<< *i << endl;
}


(page 6)


adjacent_find




(page 7)


adjacent_find example


int main() {
list l;
// fill values...
list::iterator i =
adjacent_find(l.begin(), l.end());
if (i != l.end())
cout << "first repeated num: "
<< *i << endl;
}


(page 8)


adjacent_find example 2


bool sign_change(int x, int y) {
return (x>0 && y<0) ||
(x<0 && y>0);
}


(page 9)


adjacent_find example 2


int main() {
list l;
// fill values...
list::iterator i = adjacent_find(
l.begin(), l.end(), sign_change);
if (i != l.end()) {
cout << "sign change: " << *i;
i++;
cout << " " << *i << endl;
}
}





















(page 10)


find_first_of


in: 5, 23, 6, 12, 4, 42




















(page 11)


search




(page 12)


search example


int main() {
string s = "this is a sentence";
char w[ ] = "is";
string::iterator r = search(s.begin(),
s.end(), w, w + strlen(w));
cout << "Found " << w << " at "
<< r - s.begin() << endl;
}


(page 13)


find_end




(page 14)


search_n




(page 15)


search_n example


int main() {
int A[ ] = {1,1,2,3,1,1,1,2,3,1,1,1,1,2,3};
int N = 15;
int *r = search_n(A, A + N, 4, 1);
cout << "4 1's at : "
cout << r - A << endl;
}


(page 16)


count




(page 17)


count example


int main() {
int A[ ] = {1,2,3,1,2,3,1,2,3,1,2,1,1,2,3};
int N = 15;
cout << "Number of 2's : "
<< count(A, A + N, 2) << endl;
}


(page 18)


for_each




(page 19)


for_each example


struct sum {
int sum;
sum() : sum(0) { }
void operator()(int i) { sum += i; }
};
int main() {
int A[ ] = {1,2,3,1,2,3,1,2,3,1,2,1,1,2,3};
int N = 15;
sum s = for_each(A, A + N, sum());
cout << s.sum << endl;
}


(page 20)


accumulate




(page 21)


accumulate example


#include

int main() {
double a[ ] = {1.2, 2.3, 3.4, 4.5, 5.6};
string s[ ] = {"abc", "def", "ghi", "jkl"};
cout << accumulate(a, a+5, 0.0)
<< endl;
cout << accumulate(s, s+4, string())
<< endl;
}


(page 22)


equal




(page 23)


equal example


bool compare_nocase(char c1, char c2){
return toupper(c1) == toupper(c2);
}
int main() {
string s1 = "a string";
const char *s2 = "A string";
if (equal(s1.begin(), s1.end(), s2,
compare_nocase))
cout << "Strings are equal\n";
}


(page 24)


mismatch




(page 25)


mismatch example


bool compare_nocase(char c1, char c2){
return toupper(c1) == toupper(c2);
}
int main() {
string s1 = "a string";
const char *s2 = "A string";
if (mismatch(s1.begin(), s1.end(), s2,
compare_nocase).first == s1.end())
cout << "Strings are equal\n";
}


(page 26)


lexicographical_compare




(page 27)


Example


int main() {
int A1[ ] = {3,1,4,1,4,5,9,3};
int A2[ ] = {3,1,4,1,5,0,8,2};
const int N1 = 8;
const int N2 = 8;
if (lexicographical_compare(
A1, A1 + N1, A2, A2 + N2))
cout << "A1 < A2" << endl;
else
cout << "A1 >= A2" << endl;
}


(page 28)


max_element




(page 29)


max_element example


int main() {
list l;
for(int i = 0; i < 100; ++i)
l.push_back(rand());
list::const_iterator min, max;
min = min_element(l.begin(), l.end());
max = max_element(l.begin(), l.end());
cout << "Min : " << *min << endl;
cout << "Max : " << *max << endl;
}


(page 30)


Adapters




(page 31)


binder1st and binder2nd




(page 32)


binder2nd example


int main() {
vector v;
for(int i = 0; i < 10; ++i)
v.push_back(i);
vector::iterator vi = find_if(
v.begin(), v.end(),
bind2nd(greater(), 5));
cout << "Match found at position: "
<< vi - v.begin() << '\n';
}


(page 33)


unary_negate





(page 34)


unary_negate example


int main() {
vector v;
for(int i = 0; i < 10; ++i)
v.push_back(i);
vector::iterator vi = find_if(
v.begin(), v.end(),
not1(bind2nd(greater(), 5)));
cout << "Match found at position: "
<< vi - v.begin() << '\n';
}


(page 35)


unary_compose





(page 36)


mem_fun_ref_t






(page 37)


Example


int main() {
vector v;
string s;
while (cin >> s) v.push_back(s);
vector::iterator i = find_if(
v.begin(), v.end(), compose1(
bind2nd(greater(), 7),
mem_fun_ref(&string::length)));
cout << *i << endl;
}


































































































































































































(page 38)