更易型算法,要不是直接改动元素值,就是在复制元素到另一区间的过程中改变元素值。
//对于每个测试函数, 导入以下头文件
#include
#include
#include
using namespace std;
void test(int& elem) {
elem += 1;
}
void Print(int elem) {
cout << elem << " ";
}
class Test {
public:
Test(int _val) :val(_val) {}
int operator()(int& v) const{ //仿函数
v += val;
return v;
}
private:
int val;
};
int main() {
vector<int> arr = { 1,2,3,4,5,6 };
//基本操作 通过Lambda函数
for_each(arr.begin(), arr.end(), [](int &elem) { elem += 1; }); //arr从头到尾+1
for_each(arr.begin(), arr.end(), [](int elem) {cout<<elem<<' '; }); //输出arr中每个元素 {2,3,4,5,6,7}
cout << endl;
//普通函数传入
for_each(arr.begin(), arr.end(), test);
for_each(arr.begin(), arr.end(), Print); //输出arr中每个元素 {3,4,5,6,7,8}
cout << endl;
//传入函数对象
for_each(arr.begin(), arr.end(), Test(10)); //初始化增长率为10;
for_each(arr.begin(), arr.end(), Print); //输出arr中每个元素 {13,14,15,16,17,18}
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
copy(a.begin(), a.end(), b.begin()); //b的内存要足够大使得放得下
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
copy_if(a.begin(), a.end(), b.begin(), [](int a) {return a > 3; });
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
copy_n(a.begin(),3, b.begin());
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
cout << endl;
copy_n(a.begin()+4, 3, b.begin());
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
copy_backward(a.begin(), a.end(),b.begin() + 8);
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
move(a.begin(), a.end(),b.begin());
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
int main() {
vector<int> a{ 9, 5, 4, 3, 2, 3, 1 }, b(10);
move_backward(a.begin(), a.end(),b.begin()+8);
for_each(b.begin(), b.end(), [](int i) {cout << i << ' '; });
}
将元素大写变为小写,存放指定位置
char op(char ch)
{
if(ch>='A'&&ch<='Z')
return ch+32;
else
return ch;
}
int main()
{
string first = "ABCDEFS",second;
second.resize(first.size());
transform(first.begin(),first.end(),second.begin(),op);
//transform(first.begin(), first.end(), second.begin(), tolower); //等价
cout<<second<<endl;
}
将两个数组相加后存放到一个数组中
void print(int& elem) { cout << elem << " "; }
int op(int a, int b) {
return a + b;
}
int main()
{
vector <int> A = {1,2,3,4,5,6}, B = {2,3,4,5,6,7}, SUM;
SUM.resize(A.size());
transform(A.begin(), A.end(), B.begin(), SUM.begin(), op);
for_each(SUM.begin(), SUM.end(), print);
return 0;
}
int main()
{
vector<int> first = { 1,2,3,4,5 }, second = {2,3,4,5,6}, third;
third.resize(first.size()+second.size());
merge(first.begin(), first.end(), second.begin(), second.end(), third.begin());
for_each(third.begin(), third.end(), [](int a) {cout << a << ' '; });
}
int main()
{
vector<int> first = { 1,2,3,4,5,6,7 }, second = {2,3,4,5,6,7,8,9};
swap_ranges(first.begin(), first.end(), second.begin()); //要考虑second中的交换元素要足够
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
}
int main()
{
vector<int> first = { 1,2,3,4,5,6,7 };
fill(first.begin(), first.end(), 3);
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
}
int main()
{
vector<int> first = { 1,2,3,4,5,6,7 };
fill_n(first.begin()+1, 3, 10); //不可以越界
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
}
int main()
{
int i = 0;
vector<int> first(10);
generate(first.begin(),first.end(), rand);
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
cout << endl;
generate(first.begin(), first.end(), [&i]() {++i; return i * i; });
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
}
int main()
{
vector<int> first(10);
generate_n(first.begin(),3, rand);
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
cout << endl;
generate_n(first.begin()+5, 3, [&i]() {++i; return i * i; });
for_each(first.begin(), first.end(), [](int a) {cout << a << ' '; });
}
int main()
{
char temp[100];
int n = 123;
_itoa_s(n, temp, 10);
cout << temp << endl;
_itoa_s(n, temp, 2);
cout << temp << endl;
_itoa_s(n, temp, 8);
cout << temp << endl;
_itoa_s(n, temp, 16);
cout << temp << endl;
}
int main()
{
string str = "he is a good girl";
str = str.replace(str.find("he"),2,"she"); //找到he的位置后两个字符,替换成she
cout << str << endl;
replace(str.begin(), str.end(), 'i', '#'); //i换成#
cout << str << endl;
}
int main()
{
string str = "he is a good girl";
replace_if(str.begin(), str.end(), [](char i) {return i == 'i'; }, '#'); //i换成#
cout << str << endl;
}
int main()
{
vector<int> a{ 1, 2, 4, 4, 5, 4, 8, 10 };
vector<int> b(a.size()); //vector b;
replace_copy(begin(a), end(a), b.begin(), 4, 1); //replace_copy(begin(a), end(a), back_inserter(b), 4, 1);
for_each(b.begin(), b.end(), [](int a) {cout << a << ' '; });
}
int main()
{
vector<int> a{ 1, 2, 4, 4, 5, 4, 8, 10 };
vector<int> b;
replace_copy_if(begin(a), end(a), back_inserter(b), [](int i) {return i - 4 == 0; }, 1);
for_each(b.begin(), b.end(), [](int a) {cout << a << ' '; });
}