Item 6. Containers, Pointers, and Containers That Aren t

I l @ ve RuBoard

Item 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?

  1. 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?

  2. 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.

I l @ ve RuBoard


More Exceptional C++
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
ISBN: 020170434X
EAN: 2147483647
Year: 2001
Pages: 118
Authors: Herb Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net