资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++怎么为所有可能重用的操作命名

本篇内容介绍了“C++怎么为所有可能重用的操作命名”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

柘荣网站建设公司创新互联建站,柘荣网站设计制作,有大型网站制作公司丰富经验。已为柘荣上千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的柘荣做网站的公司定做!

T.140:为所有可能重用的操作命名

Reason(原因)

Documentation, readability, opportunity for reuse.

文档化,可读性,重用的机会。

Example(示例)

struct Rec {
   string name;
   string addr;
   int id;         // unique identifier
};

bool same(const Rec& a, const Rec& b)
{
   return a.id == b.id;
}

vector find_id(const string& name);    // find all records for "name"

auto x = find_if(vr.begin(), vr.end(),
   [&](Rec& r) {
       if (r.name.size() != n.size()) return false; // name to compare to is in n
       for (int i = 0; i < r.name.size(); ++i)
           if (tolower(r.name[i]) != tolower(n[i])) return false;
       return true;
   }
);

There is a useful function lurking here (case insensitive string comparison), as there often is when lambda arguments get large.

代码中隐藏着一个有用(在不需要区分大小写时)的函数,当lambda表达式变大时通常会这样。

bool compare_insensitive(const string& a, const string& b)
{
   if (a.size() != b.size()) return false;
   for (int i = 0; i < a.size(); ++i) if (tolower(a[i]) != tolower(b[i])) return false;
   return true;
}

auto x = find_if(vr.begin(), vr.end(),
   [&](Rec& r) { compare_insensitive(r.name, n); }
);

Or maybe (if you prefer to avoid the implicit name binding to n):

或者可以这样(如果你更希望避免n上的隐式名称绑定):

auto cmp_to_n = [&n](const string& a) { return compare_insensitive(a, n); };

auto x = find_if(vr.begin(), vr.end(),
   [](const Rec& r) { return cmp_to_n(r.name); }
);
Note(注意)

whether functions, lambdas, or operators.

函数,lambda表达式,运算符都适用。

Exception(例外)

  • Lambdas logically used only locally, such as an argument to for_each and similar control flow algorithms.

    Lambda表达式逻辑上是本地使用的,例如作为一个for_each或类似的控制流算法的参数。

  • Lambdas as initializers

    Lambda表达式作为初始化器使用时。

Enforcement(实施建议)
  • (hard) flag similar lambdas

  • (困难)标记类似的lambda表达式。

“C++怎么为所有可能重用的操作命名”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


本文名称:C++怎么为所有可能重用的操作命名
本文URL:http://cdkjz.cn/article/peiiji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220