COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Mutating Algorithms




(page 2)


copy




(page 3)


copy example


int main() {
char a[ ] = "1234567890";
vector v(a, a + strlen(a));
list l1(v.size());
list l2;
copy(v.begin(), v.end(), l1.begin());
copy(v.begin(), v.end(),
back_inserter(l2));
}


(page 4)


copy_backward




(page 5)


copy_backward example


int main() {
int a[ ] = {1,2,3,4,5,6,7,8,9,10};
copy_backward(a, a + 8, a + 10);
copy(a, a + 10,
ostream_iterator(cout));
}


(page 6)


swap_ranges




(page 7)


swap_ranges example


int main() {
int a[ ] = {1, 2, 3, 4, 5};
vector v;
for (int i = 1; i <= 5; ++i)
v.push_back(i * 10);
swap_ranges(v.begin(), v.end(), a);
}


(page 8)


transform




(page 9)


transform example


int main() {
int a1[ ] = {1, 2, 3, 4, 5};
int a2[ ] = {6, 7, 8, 9, 10};
transform(a1, a1 + 5, a2,
ostream_iterator(cout, " "),
plus());
cout << endl;
transform(a1, a1 + 5,
ostream_iterator(cout, " "),
bind1st(multiplies(), 5));
}


(page 10)


replace




(page 11)


replace example


int main() {
int a1[ ] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
int a2[ ] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
int a3[10];
replace(a1, a1 + 10, 3, 10);
replace_copy(a2, a2 + 10, a3, 3, 10);
for (int i = 0; i < 10; ++i)
cout << a1[i] << '\t' << a2[i] << '\t'
<< a3[i] << endl;
}


(page 12)


replace_if


int main() {
int a[ ] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
replace_if(a, a + 10,
bind2nd(less(), 3), 10);
copy(a, a + 10,
ostream_iterator(cout, " "));
}


(page 13)


fill and fill_n




(page 14)


fill and fill_n example


int main() {
vector v(4);
fill(v.begin(), v.end(), 42);
fill_n(back_inserter(v), 4, 24);
copy(v.begin(), v.end(),
ostream_iterator(cout, " "));
}


(page 15)


generate and generate_n



int main() {
vector v(1000);
generate(v.begin(), v.end(), rand);
generate_n(
ostream_iterator(cout, "\n"),
1000, rand);
}


(page 16)


remove


new_end = remove(c.begin(), c.end(), v);
c.erase(new_end, c.end());


(page 17)


remove example


int main() {
int a[ ] = {1, 2, 1, 2, 3, 1, 2, 3, 4};
const int N = 9;
int *i = remove(a, a + N, 1);
copy(a, i,
ostream_iterator(cout, " "));
cout << endl;
copy(i, a + N,
ostream_iterator(cout, " "));
cout << endl;
}


(page 18)


remove_if




(page 19)


remove_if example


int main() {
int a[ ] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
vector v;
copy(a, a + 10, back_inserter(v));

v.erase(remove_if(v.begin(), v.end(),
bind2nd(less(), 3)));

copy(v.begin(), v.end(),
ostream_iterator(cout, " "));
}


(page 20)


unique




(page 21)


unique example


int main() {
int a[ ] = {1,1,2,1,2,3,1,1,1,2,3,2,2};
const int N = 13;
int *i = unique(a, a + N);
copy(a, i,
ostream_iterator(cout, " "));
cout << endl;
copy(i, a + N,
ostream_iterator(cout, " "));
cout << endl;
}


(page 22)


reverse




(page 23)


reverse example


int main() {
list l;
l.push_back("one");
l.push_back("two");
l.push_back("three");
reverse(l.begin(), l.end());
copy(l.begin(), l.end(),
ostream_iterator(cout, "\n"));
}


(page 24)


random_shuffle




(page 25)


random_shuffle example


int main() {
string a[ ] = { "one", "two", "three" };
const int N = 3;
random_shuffle(a, a + N);
copy(a, a + N,
ostream_iterator(cout, "\n"));
}


(page 26)


sort




(page 27)


sort example


int main() {
string a[ ] = {"one","two","three","four"};
const int N = 4;
sort(a, a + N, not2(less()));
copy(a, a + N,
ostream_iterator(cout, "\n"));
}


(page 28)


partial_sort




(page 29)


partial_sort example


int main() {
vector v(50);
generate(v.begin(), v.end(), rand);
partial_sort(v.begin(), v.begin() + 10,
v.end());
copy(v.begin(), v.end(),
ostream_iterator(cout, "\n"));
}


(page 30)


is_sorted




(page 31)


is_sorted example


template
void slow_sort(It begin, It end) {
while (!is_sorted(begin, end))
random_shuffle(begin, end);
}
int main() {
int a[ ] = {1, 2, 3, 5, 4};
const int N = 5;
slow_sort(a, a + N);
copy(a, a + N,
ostream_iterator(cout, " "));
}


(page 32)


merge


int main() {
int even[ ] = {0, 2, 4, 6, 8, 10};
int odd[ ] = {1, 3, 5, 7, 9, 11};
merge(odd, odd + 6,
even, even + 6,
ostream_iterator(cout, " "));
}


(page 33)


Useful mutating algorithms





(page 34)