deque questions

J

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?

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.

I am using "The C++ STL" by Plauger, Stepanov, Lee and Musser, but it
just doesn't explain anything.

Thanks in advance for your time.

jacob
 
A

Alf P. Steinbach

* 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
 
K

Kai-Uwe Bux

jacob said:
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?
[...]

One has to distinguish two aspects of the question: (a) what is the use of
swapping deque<T> objects and (b) why is that a member function while there
already is the free function like std::swap(). As for (b), I think an
overload for std::swap() treating deque<> efficiently would be sufficient. A
swap() member, on the other hand, allows one to avoid making the free
swap()-function a friend. Instead, the free function can just call the
member function.

As for (a), swap() is often called behind the scenes. E.g., if you had:

typedef std::deque< char > word;
typedef std::vector< word > word_list;

then sorting a word_list via

std::sort( wl.begin(), wl.end() );

will swap() words. The notion that deque<T> could be used to model items
that you may want to sort occasionally does not seem far fetched.


Best

Kai-Uwe Bux
 
J

jacob navia

Thanks for your answers. Will try to get the books recommended by Alf.

jacob
 
J

Juha Nieminen

jacob navia said:
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?

There are many practical situations where one may want to swap the
contents of two data containers. Also, swapping can often be used to
*move* all the data from one container to another (especially if the
source container is then destroyed, which will then effectively
destroy all the old data of the destination container).

The reason to have a swap() member function in STL data containers is
that's it's *way* more efficient than trying to do it in the normal way
from your own code (ie. copy the source data to a temporary data container,
then copy the destination data to the source data, and finally copy the
temporary data to the destination data). Doing it "by hand" like that would
require additional memory and would be very slow (the more the data to swap,
the slower). The swap() member functions, however, simply change some
internal pointers around, and that's it. No data is physically moved in
memory at any stage. Thus swapping usually takes just a few clock cycles
regardless of how large the data containers were.

You can imagine how this can become useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top