Chapter 6. Containers, Pointers, and Containers That Aren't
Difficulty: 5
Oil and water just don't mix. Do pointers and standard containers mix any better?
Consider the following code:
vector<char> v; // ... populate v ... char* p = &v[0]; // ... do something with *p ...
Is this code valid? Whether it's valid or not, how could it be improved?
Now consider the following code:
template<typename T> void f( T& t ) { typename T::value_type* p1 = &t[0]; typename T::value_type* p2 = &*t.begin(); // ... do something with *p1 and *p2 ... }
Is this code valid? Discuss.
Solution
Prelude
Little Stanislaus watched his mother as she rummaged busily in the refrigerator. ...
Get More Exceptional C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.