Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ name all the operations that may be reused

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how C++ names all possible reused operations". Many people will encounter this dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

T.140: name all operations that may be reused

Reason (reason)

Documentation, readability, opportunity for reuse.

Opportunities for documentation, readability, and reuse.

Example (sample)

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.

A useful function is hidden in the code (when case sensitivity is not needed), which is usually the case when lambda expressions get larger.

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):

Or you can do this (if you prefer to avoid implicit name binding on 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 (note)

Whether functions, lambdas, or operators.

Functions, lambda expressions, and operators are all applicable.

Exception (exception)

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

The Lambda expression is logically used locally, for example as a parameter to a for_each or similar control flow algorithm.

Lambdas as initializers

When Lambda expressions are used as initializers.

Enforcement (implementation recommendations)

(hard) flag similar lambdas

(difficult) Mark a similar lambda expression.

This is the end of the content of "how C++ names all possible reusable operations". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report