In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains why it is best to return to the structure or tuple when C++ returns multiple output values. The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why it is best to return to the structure or tuple when C++ returns multiple output values.
If you need to return multiple output values, it is best to return the structure or tupleReason (reason)
The return value itself indicates that it is a value used only as an output. Note that C++ can return multiple values at the same time, usually using tuple (including pair), and callers can also take advantage of tie for extra convenience. It is better to use a named structure if the return value has a specific meaning. Otherwise, the nameless tuple would be more suitable for general code.
Translator's note: tie is a new feature introduced by Clover 11 and can be used to deconstruct tuple elements.
Example (sample)
Output_data = something (); return status;}
/ / GOOD: self-documentingtuple f (const string& input) {/ /... Return make_tuple (status, something ());} translator's note: a similar practice of returning multiple values has been widely used in other languages (such as Python).
This style is already used in the standard library of Clippers 98 because pair is like a two-element tuple. For example, suppose you have a set my_set, consider the following code:
/ / C++98result = my_set.insert ("Hello"); if (result.second) do_something_with (result.first); / / workaround
You can write the result directly into a local variable that already exists with Clear11.
Sometype iter; / / default initialize if we haven't alreadySomeothertype success; / / used these variables for some other purpose
Tie (iter, success) = my_set.insert ("Hello"); / / normal return valueif (success) do_something_with (iter)
Using structured bindings 17, we can define and initialize multiple values using the structured binding feature:
If (auto [iter, success] = my_set.insert ("Hello"); success) do_something_with (iter); Exception (exception)
Sometimes we need to pass an object to the function to control the state of the object. In this case, it is usually the right way to use a reference T & to pass the object. It is generally not necessary to explicitly pass an input / output parameter on the one hand and output by returning a value on the other. For example:
For (string s; cin > > s;) {/ / do something with line}
Here s and cin are used as input / output parameters. We pass the cin through (non-constant) references to control its state. We pass s to avoid repeated requests for memory. By reusing s (passed by reference), we only re-request memory when we need to expand the capacity of s. This technique, sometimes referred to as the "output of user applications" mode, is particularly suitable for types like string and vector, which need to free up the requested storage space.
Pair get_string (istream& is); / / not recommended {string s; is > > s; return {is, s};}
For (auto p = get_string (cin); p.first;) {/ / do something with p.second}
If you really strictly understand this guideline (F.21), this exception is not a real exception, because it depends on input / output parameters rather than the simple output parameters mentioned in this guideline. However, what we emphasize is that
A situation that is conveyed explicitly rather than implicitly.
Note (Note)
In many cases, it may be useful to pass a clear, user-defined type. For example:
Struct Distance {int value; int unit = 1; / / 1 means meters}
Distance D1 = measure (obj1); / / access d1.value and d1.unitauto D2 = measure (obj2); / / access d2.value and d2.unitauto [value, unit] = measure (obj3); / / access value and unit; somewhat redundant / / to people who know measure () auto [x, y] = measure (obj4); / / don't; it's likely to be confusing
Translator's note: the use of [XMague y] in the code is the structured binding (structred binding) introduced in Category 17.
General pair and tuple should only be used to return values that represent separate entities (there is no internal connection between the data), not some abstraction.
Another example is to use a specific type similar to variant instead of using a generic tuple.
Enforcement (implementation recommendations)
You should use the return value instead of the output parameter. The output parameter can be the object that the function writes to the action, calls a non-constant member function, or is passed as a non-constant quantity.
Thank you for your reading, the above is the content of why it is best to return structure or tuple when C++ returns multiple output values. After the study of this article, I believe you have a deeper understanding of why it is best to return structure or tuple when C++ returns multiple output values, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.