Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers (C++ in-Depth Series) by Gottschling Peter

Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers (C++ in-Depth Series) by Gottschling Peter

Author:Gottschling, Peter [Gottschling, Peter]
Language: eng
Format: azw3, epub
Publisher: Pearson Education
Published: 2015-12-22T16:00:00+00:00


vv.push_back(v1);

vv.push_back(v2);

vv.push_back(v3);

vv.push_back(v2);

vv.push_back(v1);

They are implicitly converted into reference wrappers (reference_wrapper<T> contains a constructor for T& that is not explicit).

The class contains a method get to get a reference to the actual object so that we can, for instance, print our vector:

Click here to view code image

for (const auto& vr : vv) {

copy(begin(vr.get()), end(vr.get()),

ostream_iterator<int>(cout, ", "));

cout endl;

}

Here, the type of vr is const reference_wrapper<vector<int> >&. The wrapper also provides an implicit conversion to the underlying reference type T& that can be used more conveniently:

Click here to view code image

for (const vector<int>& vr : vv) {

copy(begin(vr), end(vr), ostream_iterator<int>(cout, ", "));

cout endl;

}

The wrapper is complemented by two helpers: ref and cref, found in the same header. ref yields for an lvalue of type T an object of type reference_wrapper<T> referring to the former. If the argument of ref is already a reference_wrapper<T>, then it is just copied. Likewise, cref yields an object of type reference_wrapper<const T>. These functions are used in several places in the standard library.

We will use them for creating a std::map of references:

Click here to view code image

map<int, reference_wrapper<vector<int> > > mv;

Given the length of the wrapper’s type name, it is shorter to declare it with type deduction like this:

Click here to view code image

map<int, decltype(ref(v1))> mv;

The usual bracket notation of maps:

mv[4]= ref(v1); // Error

cannot be applied since the wrapper has no default constructor which is called internally in the expression mv[4] before the assignment takes place. Instead of the bracket notation, we should use insert or emplace:

Click here to view code image

mv.emplace(make_pair(4, ref(v1)));

mv.emplace(make_pair(7, ref(v2)));

mv.insert(make_pair(8, ref(v3)));

mv.insert(make_pair(9, ref(v2)));

To iterate over the entries, it is again easier to apply type deduction:

Click here to view code image

for (const auto& vr : mv) {

cout vr.first ": ";

for (int i : vr.second.get())

cout i ", ";

cout endl;

}

As the bracket operator of our map doesn’t compile, searching for a specific entry is performed with find :

auto& e7= mv.find(7)->second;

This yields a reference to the vector associated with the key 7.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.