长得像函数,但是不是函数
实际上就是在类或结构体中重载了()运算符
struct aMax {
int operator()(int a, int b) {
return a > b ? a : b;
}
};
class Max{
public:
int operator()(int a, int b) {
return a > b ? a : b;
}
};
int main() {
//仿函数
int a = Max()(5, 6);
int b = aMax()(9, 10);
std::cout << a << std::endl;
std::cout << b << std::endl;
return 0;
}
基本概念这里就不浪费时间介绍了,代码中有介绍到,这里给出空间适配器的基本使用方式:
#include
#include
class Max{
public:
int operator()(int a, int b) {
return a > b ? a : b;
}
};
struct aMax {
int operator()(int a, int b) {
return a > b ? a : b;
}
};
int main() {
//仿函数
int a = Max()(5, 6);
int b = aMax()(9, 10);
std::cout << a << std::endl;
std::cout << b << std::endl;
//控制适配器
//控制适配器的类型
std::allocator<int> obj;
int* p = obj.allocate(1);//这里实际上是申请了1个int的空间
obj.construct(p, 12138);//将p地址的值修改为12138
std::cout << *p << std::endl;
*p = 100;//直接解引用也可以使用
std::cout << *p << std::endl;
int temp = 500;
//address方法:获取指定类型,指定元素的指针,返回值类型:T*
std::cout << typeid(obj.address(temp)).name() << std::endl;
int *bAddr =obj.address(temp);
std::cout << bAddr << "\t" << *bAddr << std::endl;
//max_size方法,返回适配器的最大尺寸,返回值类型:unsigned int
std::cout << typeid(obj.max_size()).name() << std::endl;
std::cout << obj.max_size() << std::endl;
//destory方法:释放对象空间
obj.destroy(p);
//deallocate方法:释放指定空间,指定大小
obj.deallocate(p, 1);
return 0;
}
基本概念这里就不浪费时间介绍了,代码中有介绍到:
#include
#include
#include
struct Func {
bool operator()(int a) {
return a == 6;
}
};
bool Fun5(int a) {
return a == 5;
}
template <typename T>
void print(T Begin, T End);
void print1(int a);
int main() {
std::vector<int> oec = { 0,4,2,3,6,8,7,51,2,2,2,2,2,2 };
//find算法:这里介绍两种,第一种:find(起始迭代器,终止迭代器,要寻找的值)
auto it = std::find(oec.begin(), oec.end(), 6);
std::cout << typeid(it).name() << std::endl;
if (it != oec.end()) {
std::cout << "successful" << std::endl;
}
else {
std::cout << "failed" << std::endl;
}
//第二种:find_if(起始迭代器,终止迭代器,仿函数/函数)
auto it1 = std::find_if(oec.begin(), oec.end(), Func());
if (it1 != oec.end()) {
std::cout << "successful" << std::endl;
}
else {
std::cout << "failed" << std::endl;
}
auto it2 = std::find_if(oec.begin(), oec.end(), Fun5);
if (it2 != oec.end()) {
std::cout << "successful" << std::endl;
}
else {
std::cout << "failed" << std::endl;
}
//在父串中找字串,返回值类型:迭代器类型,指向第一次出现的位置
std::vector<int> obj = { 2,3 };
std::find_first_of(oec.begin(), oec.end(), obj.begin(), obj.end());
std::cout << typeid(std::find_first_of(oec.begin(), oec.end(), obj.begin(), obj.end())).name() << std::endl;
//search_n方法:
std::search_n(oec.begin(), oec.end(), 4, 2);
std::cout << typeid(std::search_n(oec.begin(), oec.end(), 4, 2)).name() << std::endl;
//count方法:统计容器中,某个元素出现的次数,返回值类型:int
std::count(oec.begin(), oec.end(), 2);
std::cout << typeid(std::count(oec.begin(), oec.end(), 2)).name() << std::endl;
std::cout << std::count(oec.begin(), oec.end(), 2) << std::endl;
//count_id方法,与上述find与find_if方法差别相似
std::count_if(oec.begin(), oec.end(), Func());
std::cout << typeid(std::count_if(oec.begin(), oec.end(), Func())).name() << std::endl;
std::cout << std::count_if(oec.begin(), oec.end(), Func()) << std::endl;
//排序算法
//1.排序,默认greater排序
std::sort(oec.begin(), oec.end());
print(oec.begin(), oec.end());
std::cout << "*********************************************************************" << std::endl;
//2.显示指定排序规则
std::sort(oec.begin(), oec.end(), std::greater<int>());
print(oec.begin(), oec.end());
std::cout << "*********************************************************************" << std::endl;
//从begin迭代器到end迭代器,每一个元素都执行print1函数
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
//堆排序
//1.
std::make_heap(oec.begin(), oec.end(), std::greater<int>());
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
//2.
std::sort_heap(oec.begin(), oec.end(), std::greater<int>());
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
//replace算法:修改容器中的值
std::replace(oec.begin(), oec.end(), 3, 12345);
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
fill算法:将所有元素重新赋值
//std::fill(oec.begin(), oec.end(), 555);
//std::for_each(oec.begin(), oec.end(), print1);
//std::cout << std::endl;
//std::cout << "*********************************************************************" << std::endl;
//remove算法:删除指定元素
std::remove(oec.begin(), oec.end(), 12345);
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
//reverse算法:将所有元素逆序
std::reverse(oec.begin(), oec.end());
std::for_each(oec.begin(), oec.end(), print1);
std::cout << std::endl;
std::cout << "*********************************************************************" << std::endl;
return 0;
}
template<typename T>
void print(T Begin, T End)
{
T i;
for (i = Begin; i != End; i++) {
std::cout << *i << std::endl;
}
std::cout << std::endl;
}
void print1(int a)
{
std::cout << a << "\t";
}
如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!