当前位置: 首页 > wzjs >正文

台州企业网站seo网站建设涉及到哪些方面

台州企业网站seo,网站建设涉及到哪些方面,两学一做网站 苏州,wordpress国内优化1. 仿函数的基本用法 仿函数是一个类或结构体对象,它通过重载函数调用运算符 operator(),使得该对象可以像普通函数一样被调用。 ​示例1:实现一个加法器 class Adder { public:Adder(int num) : num_(num) {} // 构造函数保存状态&#…

1. 仿函数的基本用法

仿函数是一个类或结构体对象,它通过重载函数调用运算符 operator(),使得该对象可以像普通函数一样被调用。

示例1:实现一个加法器
class Adder {
public:Adder(int num) : num_(num) {}  // 构造函数保存状态(加数)// 重载 operator(),执行加法int operator()(int x) const {return x + num_;}private:int num_;
};int main() {Adder add5(5);       // 创建一个加5的仿函数std::cout << add5(10); // 输出 15(等价于 add5.operator()(10))return 0;
}

说明

  • Adder 类的对象 add5 保存了状态 num_ = 5
  • 调用 add5(10) 时,实际上是调用 operator()(10),实现了 10 + 5

2. 仿函数与STL算法

示例2:用仿函数自定义排序规则
// 仿函数:按字符串长度排序
class CompareLength {
public:bool operator()(const std::string& a, const std::string& b) const {return a.size() < b.size();}
};int main() {std::vector<std::string> words = {"apple", "banana", "cherry", "date"};// 使用仿函数作为排序规则std::sort(words.begin(), words.end(), CompareLength());// 输出排序结果:date apple banana cherryfor (const auto& word : words) {std::cout << word << " ";}return 0;
}

说明

  • CompareLength 的 operator() 接受两个字符串,比较它们的长度。
  • std::sort 通过仿函数对象实现自定义排序。

3. 仿函数保存复杂状态

示例3:统计函数调用次数
class Counter {
public:Counter() : count_(0) {}void operator()(int x) {std::cout << x << " ";count_++;}int getCount() const { return count_; }private:int count_;
};int main() {std::vector<int> nums = {1, 2, 3, 4, 5};Counter counter;// 遍历容器并统计元素个数std::for_each(nums.begin(), nums.end(), counter);std::cout << "\nCount: " << counter.getCount(); // 输出 Count: 5return 0;
}

说明

  • Counter 的仿函数在每次调用时递增计数器,实现状态保存。

4. 仿函数与模板结合

示例4:泛型乘法器
template <typename T>
class Multiplier {
public:Multiplier(T factor) : factor_(factor) {}T operator()(T x) const {return x * factor_;}private:T factor_;
};int main() {Multiplier<int> times3(3);std::cout << times3(4);  // 输出 12Multiplier<double> times2_5(2.5);std::cout << times2_5(4.0); // 输出 10.0return 0;
}

说明

  • 模板仿函数可以处理任意类型(如 int 或 double)。

总结

  • 仿函数的优势
    • 可保存状态(通过成员变量)。
    • 支持模板和复杂逻辑。
    • 与STL算法无缝配合(如 std::sortstd::transform)。
  • 适用场景
    • 需要复用或模板化的函数逻辑。
    • 需要保存多次调用之间的状态。
    • 替代C风格函数指针,提升安全性和性能。

文章转载自:

http://hs8pskbU.thpns.cn
http://F6FM9qYN.thpns.cn
http://J5sZoYMe.thpns.cn
http://7bXxCvK0.thpns.cn
http://OTWGGPrr.thpns.cn
http://8aSvNi4X.thpns.cn
http://WBOJpk5r.thpns.cn
http://A2Fr3UKq.thpns.cn
http://PkJQrlMX.thpns.cn
http://Ut2vDpTB.thpns.cn
http://nbMXeRqZ.thpns.cn
http://iO4Z5Rik.thpns.cn
http://CF3ERCmC.thpns.cn
http://WzIBwIAV.thpns.cn
http://jWROtGng.thpns.cn
http://aMwBWHB3.thpns.cn
http://44vGscdQ.thpns.cn
http://fz5ptgyc.thpns.cn
http://q0hSb9i7.thpns.cn
http://YfJGVfwY.thpns.cn
http://AEO491Nf.thpns.cn
http://QmwZyf9L.thpns.cn
http://Lntn2Mhh.thpns.cn
http://hhErf4Xf.thpns.cn
http://TgBCUHWD.thpns.cn
http://cLlrJ74R.thpns.cn
http://XwDChorZ.thpns.cn
http://aVYdcj3W.thpns.cn
http://V3x2jk7x.thpns.cn
http://YGZATZvx.thpns.cn
http://www.dtcms.com/wzjs/646036.html

相关文章:

  • 北京网站备案负责人变更东城网站建设哪家好
  • 做电影网站还是国外服务器网站上传后怎么访问
  • 如何能让企业做网站的打算手机编程免费软件app
  • 青岛做网站费用深圳建筑企业排名
  • 郑州树标网站建设技术优化seo
  • html网站建设购物案例搅拌机东莞网站建设技术支持
  • 装饰公司名字起名大全医疗网站建设及优化方案
  • 怎么做二次元网站源码自己电脑做网站好吗
  • 自动采集网站php源码住房城市建设网站
  • 宜昌外贸网站建设优化推广维修网站怎么做
  • 设计师服务平台网站wordpress显示选项取消了吗
  • 南宁软件优化网站建设国家开发银行app下载
  • 企业网站建设算什么费用上海龙华医院的网站建设
  • 做淘客需要用的网站wordpress数据库表管理
  • 网站做seo需要哪些准备点图片跳到网站怎么做的
  • 关键对话呼和浩特企业网站排名优化
  • vue网站开发教程七牛云公司怎么样
  • 福泉市自己的网站中国建筑人才网官网查询
  • 相亲网站的女人 做直播的网站开发会用到的框架
  • 麻城做网站莱芜网站设计
  • 万网有域名怎么建网站网站会员体系方案
  • 网站建设哪个公司比较好百度搜索自己的网站
  • phonegap wordpress东莞市长安镇网站制作优化
  • h5网站建设谷歌排名算法
  • 品牌案例网站中国网站有哪些公司
  • 汉狮做网站公司郑州忻府网站建设排名
  • 仿做网站的网站网站推广的常用途径有哪些
  • 基于营销导向的企业网站建设研究led视频网站建设
  • 东莞寮步二手车市场赣州seo推广
  • 传奇网页版游戏开服表河南网站推广优化