How does C++ name all the operations that may be reused
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!