1.for_each
2.copy,copy_back_ward
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
void print_element(int n) {
cout << n << " ";
}
bool greater_than_3(int n) {
return n > 3;
}
void add_3(int& n) {
n += 3;
}
int main(void) {
int a[] = { 1, 2, 3, 4, 5 };
vector<int> v(a, a+5);
list<int> l(15);
//list<int> l;
for_each(l.begin(), l.end(), print_element);
cout << endl;
for_each(v.begin(), v.end(), print_element);
cout << endl;
for_each(v.begin(), v.end(), add_3);
for_each(v.begin(), v.end(), print_element);
cout << endl;
copy(v.begin(), v.end(), l.begin());
//list<int>l必须是有空间
for_each(l.begin(), l.end(), print_element);
cout << endl;
for_each(v.begin(), v.end(), print_element);
cout << endl;
auto ite = l.end();
ite--;
copy_backward(v.begin(), v.end(), ite);
//拷贝的时候,往前面拷贝的
for_each(l.begin(), l.end(), print_element);
cout << endl;
return 0;
}
3.transform
template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform ( InputIterator first1, // 源容器的起始地址
InputIterator last1,// 源容器的终止地址
OutputIterator result,// 目标容器的起始地址
UnaryOperator op );// 函数指针
typedef 目标容器元素类型 (*UnaryOperator)(源容器元素类型);
template < class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperator >
OutputIterator transform ( InputIterator1 first1,// 源容器1的起始地址
InputIterator1 last1,// 源容器1的终止地址
InputIterator2 first2,// 源容器2的起始地址,元素个数与1相同
OutputIterator result,// 目标容器的起始地址,元素个数与1相同
BinaryOperator binary_op );// 函数指针
typedef 目标容器元素类型 (*BinaryOperator)(源容器1元素类型,源容器2元素类型);
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
void print_element(int n) {
cout << n << " ";
}
bool greater_than_3(int n) {
return n > 3;
}
void add_3(int& n) {
n += 3;
}
int fun(int n) {
return 2 * n;
}
int fun2(int a,int b) {
return a+b;
}
int main(void) {
int a[] = { 1, 2, 3, 4, 5 };
vector<int> v(a, a+5);
list<int> l(15);
list<int> l2(15);
//list<int> l;
transform(v.begin(), v.end(), v.begin(), fun);
for_each(l.begin(), l.end(), print_element);
cout << endl;
for_each(v.begin(), v.end(), print_element);
cout << endl;
transform(v.begin(), v.begin()+2, v.begin()+3, l2.begin(), fun2);
for_each(l2.begin(), l2.end(), print_element);
cout << endl;
return 0;
}
4.replace,replace_copy,replace_copy_if
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
void print_element(int n) {
cout << n << " ";
}
bool func(int n) {
// if(13 == n) {
// return true;
// }
return n < 10;
}
int main(void) {
int a[] = { 1, 2, 3, 4, 5 };
vector<int> v(a, a+5);
list<int> l(15);
replace(v.begin(), v.end(), 3, 13);
for_each(v.begin(), v.end(), print_element);
cout << endl;
replace_copy(v.begin(), v.end(), l.begin(), 13, 3);
for_each(l.begin(), l.end(), print_element);
cout << endl;
replace(l.begin(), l.end(), 3, 13);
for_each(l.begin(), l.end(), print_element);
cout << endl;
for_each(v.begin(), v.end(), print_element);
cout << endl;
replace_copy_if(v.begin(), v.end(), l.begin(), func, 999);
for_each(l.begin(), l.end(), print_element);
cout << endl;
return 0;
}