目录
4 STL-函数对象
237 函数对象
谓词
238 一元谓词
239 二元谓词
内建函数对象
240 算术仿函数
241 关系仿函数
242 逻辑仿函数
5 STL-常用算法
遍历算法
243 for_each
244 transform
查找算法
245 find
246 find_if
247 adjacent_find
248 binary_search
249 count
250 count_if
排序算法
251 sort
252 random_shuffle
253 merge
254 reverse
拷贝和替换算法
255 copy
256 replace
257 replace_if
258 swap
算术生成算法
259 accumulate
260 fill
集合算法
261 set_intersection
262 set_union
263 set_difference
//可以有参数,可以有返回值
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
//可以有自己的状态
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count;//可以记录调用次数
};
void doPrint(MyPrint &mp,string test)
{
mp(test);
}
//函数对象可以作为参数传递
void test()
{
MyPrint myprint;
doPrint(myprint, "hellosfwe");
}
//查找有没有大于5的元素
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vectorv;
for (int i = 0; i <10; i++)
{
v.push_back(i);
}
vector::iterator it=find_if(v.begin(),v.end(),GreaterFive());
if (it == v.end())
{
cout << "no" << endl;
}
else
{
cout << "yes:" << *it<
class MyCompare
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{
vectorv;
v.push_back(10);
v.push_back(5);
v.push_back(15);
sort(v.begin(), v.end());
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;//5 10 15
//使用函数对象 改变排序策略 变为降序
sort(v.begin(), v.end(), MyCompare());
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;//15 10 5
}
//negate 一元仿函数 取反
negaten;
cout<p;
cout << p(1, 5) << endl;//6
//关系仿函数 大于 greater
vectorv;
v.push_back(10);
v.push_back(5);
v.push_back(15);
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;//10 5 15
sort(v.begin(), v.end());
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;//5 10 15
sort(v.begin(), v.end(), greater());
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;// 15 10 5
vectorv;
v.push_back(true);
v.push_back(false);
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;//1 0
//将v搬运到容器v2中并执行取反操作
vectorv2;
v2.resize(v.size());//开辟空间
transform(v.begin(), v.end(), v2.begin(), logical_not());
for (vector::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
cout << *it2 << " ";
}
cout << endl;//0 1
void myPrint(int val)
{
cout << val <<" ";
}
class MyPrint
{
public:
void operator()(int val)
{
cout << val <<" ";
}
};
void test01()
{
vectorv;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), myPrint);
cout << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
/*
transform算法 将指定容器区间元素搬运到另一容器中
注意 : transform 不会给目标容器分配内存,所以需要我们提前分配好内存
*/
class MyTransform
{
public:
int operator()(int val)
{
return val+100;//搬运时加100
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val <<" ";
}
};
void test01()
{
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
vectorvTarget;
//重新指定 vTarget大小
vTarget.resize(v1.size());
transform(v1.begin(), v1.end(), vTarget.begin(), MyTransform());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
//100 101 102 103 104 105 106 107 108 109
}
//查找有没有5这个元素
vector::iterator it = find(v1.begin(), v1.end(), 5);
if (it != v1.end())
{
cout << "找到了元素: " << *it << endl;
}
else
{
cout << "未找到" << endl;
}
//查找自定义数据类型(需要重载==号)
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person & p)//重载==号,让底层find知道如何对比person数据类型
{
return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
}
string m_Name;
int m_Age;
};
void test02()
{
vectorv;
Person p1("aaa", 10);
Person p2("bbb", 40);
Person p3("ccc", 20);
Person p4("ddd", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector::iterator it = find(v.begin(), v.end(), p3);
if (it != v.end())
{
cout << "找到了元素---姓名: " << (*it).m_Name << " 年龄: "<< it->m_Age << endl;
}
else
{
cout << "未找到" << endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class MyComparePerson :public binary_function
{
public:
bool operator()(Person* p1, Person* p2) const
{
return (p1->m_Name == p2->m_Name && p1->m_Age == p2->m_Age);
}
};
void test01()
{
vectorv;
Person p1("aaa", 10);
Person p2("bbb", 40);
Person p3("ccc", 20);
Person p4("ddd", 30);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
Person* p = new Person("bbb", 40);
vector::iterator it = find_if(v.begin(), v.end(), bind2nd(MyComparePerson(), p));
if (it != v.end())
{
cout << "找到了数据 姓名: " << (*it)->m_Name << " 年龄: " << (*it)->m_Age << endl;
}
else
{
cout << "未找到" << endl;
}
}
//从大到小
sort(v1.begin(), v1.end(), greater());
void test01()
{
srand((unsigned int)time(NULL));
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//random_shuffle算法 对指定范围内的元素随机调整次序
random_shuffle(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
cout << endl;
}
//merge算法 容器元素合并,并存储到另一容器中
//注意 : 两个容器必须是有序的
void test01()
{
vectorv1;
vectorv2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 3);
}
vectorvTarget;//目标容器
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), [](int val) {cout << val << " "; });
//0 1 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12
}
//reverse算法 反转指定范围的元素
void test04()
{
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
v1.push_back(3);
reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
cout << endl;
//3 9 8 7 6 5 4 3 2 1 0
}
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
vectorvTarget;
vTarget.resize(v1.size());
//copy算法 将容器内指定范围的元素拷贝到另一容器中
copy(v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), [](int val){ cout << val << " "; });
cout << endl;
//0 1 2 3 4 5 6 7 8 9
//replace算法 将容器内指定范围的旧元素修改为新元素
//replace_if算法 将容器内指定范围满足条件的元素替换为新元素
class MyCompare
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
replace(v1.begin(), v1.end(), 3, 100);//0 1 2 100 4 5 6 7 8 9
copy(v1.begin(), v1.end(), ostream_iterator(cout, " "));
cout << endl;
//按条件 进行替换 将所有大于5 替换为 3000
replace_if(v1.begin(), v1.end(), MyCompare(), 3000);//0 1 2 3000 4 5 3000 3000 3000 3000
copy(v1.begin(), v1.end(), ostream_iterator(cout, " "));
}
两个容器需要同类型