# Algorithms
```
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <list>
#include <vector>
bool even(int n)
{
return ((n % 2) == 0);
}
struct lex {
int num;
char name[10];
} s[26], s2[26];
int cmp(lex a, lex b)
{
return strcmp(a.name, b.name) > 0;
}
int cmp2(lex a, lex b)
{
return a.num>b.num ? 1 : 0;
}
void findif(); //find_if
void COPY(); //copy
void SWAP(); //swap
void repl(); //replace
void FILL(); //fill
void SORT(); //sort
void stab(); //stable_sort
void low(); //lower_bound
void up(); //upper_bound
void bin(); //binary_search
void MIN(); //min()
void MAX(); //max()
using namespace std;
int main()
{
cout << "find_if=";
findif();
cout << "=========================" << endl << "copy=";
COPY();
cout << "=========================" << endl << "swap=";
SWAP();
cout << "=========================" << endl << "replace=";
repl();
cout << "=========================" << endl << "fill=";
FILL();
cout << "=========================" << endl << "sort=";
SORT();
cout << "=========================" << endl << "stable_sort=";
stab();
cout << "=========================" << endl << "lower_bound=";
low();
cout << "=========================" << endl << "upper_bound=";
up();
cout << "=========================" << endl << "binary_search=";
bin();
cout << "=========================" << endl << "min=";
MIN();
cout << "=========================" << endl << "max=";
MAX();
return 0;
}
void findif()
{
vector<int> v = {1, 2, 3, 4, 5};
auto it = find_if(v.begin(), v.end(), even);
if (it != end(v))
cout << "偶數=" << *it << endl;
}
void COPY()
{
int a[3] = {1, 2, 3};
int b[3];
copy(a, a + 3, b);
for(int j = 0; j < 3; j++)
cout << b[j] << endl;
}
void SWAP()
{
int a = 999, b = 111;
swap(a, b);
cout << "a=" << a << " b=" << b << endl;
}
void repl()
{
string a = "C++";
a.replace(1, 2, "#");
cout << a << endl;
}
void FILL()
{
int a[5];
fill(a, a + 5, 9);
for(int i = 0; i < 5; i++)
cout << a[i] << endl;
}
void SORT()
{
int i;
for(i = 65; i < 87; i++) {
s[i - 65].name[0] = (char)i;
s[i - 65].num = i - 65;
}
s[22].name[0] = 'a';
s[23].name[0] = 'a';
s[24].name[0] = 'a';
s[25].name[0] = 'a';
sort(s, s + 26, cmp);
for(i = 0; i < 26; i++) {
cout << s[i].name << " " << s[i].num << endl;
}
cout << "第二次" << endl;
sort(s, s + 26, cmp2);
for(i = 0; i < 26; i++) {
cout << s[i].name << " " << s[i].num << endl;
}
}
void stab()
{
int i;
for(i = 65; i < 87; i++) {
s2[i - 65].name[0] = (char)i;
s2[i - 65].num = i - 65;
}
s2[22].name[0] = 'a';
s2[23].name[0] = 'a';
s2[24].name[0] = 'a';
s2[25].name[0] = 'a';
stable_sort(s2, s2 + 26, cmp);
for(i = 0; i < 26; i++) {
cout << s2[i].name << " " << s2[i].num << endl;
}
cout << "第二次" << endl;
stable_sort(s2, s2 + 26, cmp2);
for(i = 0; i < 26; i++) {
cout << s2[i].name << " " << s2[i].num << endl;
}
}
void low()
{
int a[5] = {100, 25, 56, 13, 59};
sort(a, a + 5);
cout << (lower_bound(a, a + 5, 50) - a) << endl;
}
void up()
{
int a[5] = {100, 25, 56, 13, 59};
sort(a, a + 5);
cout << (lower_bound(a, a + 5, 50) - a) << endl;
}
void bin()
{
int a[5] = {2189, 34, 93, 238, 365};
sort(a,a+5);
if(binary_search(a, a + 5, 93))
cout << "有93" << endl;
else
cout << "沒有93" << endl;
}
void MIN()
{
int a = 10, b = 20;
cout << min(a, b) << endl;
}
void MAX()
{
int a = 10, b = 20;
cout << max(a, b) << endl;
}
```