Re: deque questions
* jacob navia:
Hi C++ wizards
:-)
I am studying the deque container, and I am wondering what is the use of
some member functions.
Specifically the "swap" member. I understand what it does, but I fail to
see the utility. Why it is important to have this "swap" member? What
could be the possible uses of swapping two deque containers?
For example (off the cuff) ...
class Bongo
{
private:
std::deque<Foo> myDeque;
Bar* myBar;
public:
Bongo(): myBar( new Bar ) {}
Bongo( Bongo const& other )
: myDeque( other.myDeque )
, myBar( new Bar( *other.myBar ) )
{}
void swap( Bongo& other )
{
std::swap( myDeque, other.myDeque );
std::swap( myBar, other.myBar );
}
Bongo& operator=( Bongo other )
{
swap( other ); return *this;
}
};
.... lets you express an otherwise tricky assignment operator in terms of copy
construction.
In a more general fashion, is there a document or book that would
explain WHY the STL was designed like it is? I mean a book/document that
would try to answer questions like this.
If by "STL" you mean the STL subset of the C++ standard library, then it might
be fruitful to search for writings by Stepanov, who designed it.
As I recall Stepanov and Stroustrup together made the proposal to incorporate
the STL into the C++ standard library.
So perhaps they've written something together, but for the original rationales
I'd look up Stepanov's writings.
I am using "The C++ STL" by Plauger, Stepanov, Lee and Musser, but it
just doesn't explain anything.
I think the book by Josuttis is the recommended one.
Or perhaps the template programming book by Josuttis and Vandevoorde.
I don't have either one, though.
Thanks in advance for your time.
Cheers & hth.,
- Alf