A function can return simplest a unmarried cost, but that price can be a category object with
Question:
A function can return simplest a unmarried cost, but that price can be a category object
with many contributors. This lets in us to efficiently return many values. For
example:
Click right here to view code image
struct Entry
string name;
int cost;
;
Entry read_entry(istream& is) // naive read feature (for a
higher model, see §10.Five)
string s;
int i;
is >> s >> i;
return s,i;
car e = read_entry(cin);
cout << " " << e.Name << "," << e.Price << " n";
Here, s,i is used to construct the Entry go back value. Similarly, we are able to
"unpack" an Entry's members into nearby variables:
Click here to view code image
car [n,v] = read_entry(is);
cout << " " << n << "," << v << " n";
The auto [n,v] declares neighborhood variables n and v with their sorts deduced
from read_entry()'s go back kind. This mechanism for giving local names to
members of a class item is known as established binding.
Consider every other example:
Click right here to view code photograph
map
// ... Fill m ...
For (const car [key,value] : m)
cout << "" << key "," << value << "n";
As common, we will enhance automobile with const and &. For example:
Click right here to view code photo
void incr(map
element of m
for (car& [key,value] : m)
++price;
When structured binding is used for a class without a non-public data, it is easy to
see how the binding is completed: there ought to be the equal range of names defined