How to use span or span_p
This article mainly introduces "how C++ uses span or span_p". In daily operation, I believe many people have doubts about how C++ uses span or span_p. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how C++ uses span or span_p". Next, please follow the editor to study!
F.24: use span or span_p to represent semi-open sequences
Half-open is a mathematical concept, which means that when an array in C++ is represented by [p:p+n), p points to the first element, and pairn is outside the array.
Reason (reason)
Informal/non-explicit ranges are a source of errors.
Informal, unclear scope is the source of errors.
Example (sample)
X* find (span r, const X & v); / / find v in r
Vector vec;//... auto p = find ({vec.begin (), vec.end ()}, X {}); / / find X {} in veNote (Note)
The concept of scope is particularly common in C++ code. Typically, the scope is not explicit and it is difficult to confirm whether it is used properly. In particular, given a pair of representation parameters (p, n) to represent an array [n:p+n), it is usually impossible to know whether there are really n elements available after p. As a simple helper class, span is used to indicate the scope [ptrue Q), while the range used by span_p to represent begins with p and ends with the element whose first predicate is PRQ.
Translator's note: it is a pity that no examples of the use of span_p have been found.
Example (sample)
Span represents the scope of the element, but how do we manipulate the elements in the scope?
Void f (span s) {/ / range traversal (guaranteed correct) for (int x: s) cout
This article mainly introduces "how C++ uses span or span_p". In daily operation, I believe many people have doubts about how C++ uses span or span_p. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how C++ uses span or span_p". Next, please follow the editor to study!
F.24: use span or span_p to represent semi-open sequences
Half-open is a mathematical concept, which means that when an array in C++ is represented by [p:p+n), p points to the first element, and pairn is outside the array.
Reason (reason)
Informal/non-explicit ranges are a source of errors.
Informal, unclear scope is the source of errors.
Example (sample)
X* find (span r, const X & v); / / find v in r
Vector vec;//... auto p = find ({vec.begin (), vec.end ()}, X {}); / / find X {} in veNote (Note)
The concept of scope is particularly common in C++ code. Typically, the scope is not explicit and it is difficult to confirm whether it is used properly. In particular, given a pair of representation parameters (p, n) to represent an array [n:p+n), it is usually impossible to know whether there are really n elements available after p. As a simple helper class, span is used to indicate the scope [ptrue Q), while the range used by span_p to represent begins with p and ends with the element whose first predicate is PRQ.
Translator's note: it is a pity that no examples of the use of span_p have been found.
Example (sample)
Span represents the scope of the element, but how do we manipulate the elements in the scope?
Void f (span s) {/ / range traversal (guaranteed correct) for (int x: s) cout