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
Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers (C++ in-Depth Series) by Gottschling Peter.epub
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.
C | C++ |
Tutorials | Visual C++ |
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6781)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6508)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6376)
Kotlin in Action by Dmitry Jemerov(5061)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3975)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3761)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3702)
The Ultimate iOS Interview Playbook by Avi Tsadok(3678)
