map and multimap

A

al

When should use multimap?

map template is as below:

namespace std {
template<class Key, class T, class Pred, class A>
class map;

What are "class Pred" and "class A" for?

When not to use STL at all?

Thanks!
 
R

Ron Natalie

al said:
What are "class Pred" and "class A" for?

Map's are ordered containers. Pred is the "predicate" function for providing
that ordering. It's defaulted to "less" which just uses < (which works for any
type that defines that).

A is the allocator function which provides memory for the container's dynamic
parts. Again, this is defaulted and unless you spefically need to control where
things get allocated, you can just leave it defaulted.
When not to use STL at all?
It's rare I don't use the standard library. I'm not even sure what you are
asking by that question.
 
A

al

Ron Natalie said:
Map's are ordered containers. Pred is the "predicate" function for providing
that ordering. It's defaulted to "less" which just uses < (which works for any
type that defines that).

A is the allocator function which provides memory for the container's dynamic
parts. Again, this is defaulted and unless you spefically need to control where
things get allocated, you can just leave it defaulted.
Thanks a lot!

Is A really a function since is "class A"? Why is it needed to provide
memory for the container?

vector has a member function, reserve(). Is it always necessary to call it
when creating a vector container?
 
R

Ron Natalie

al said:
memory for the container?
No, it's a class. I mispoke. Every time you add stuff to the container, it
is copied into it. The memory has to come from somewhere.
vector has a member function, reserve(). Is it always necessary to call it
when creating a vector container?

Nope, it's not normally necessary to call it at all. It just tells the vector, hey
allocate enough memory for this many all at once so you won't have to do it
piece meal as I add things to the vector.
 
N

Nick Hounsome

al said:
When should use multimap?

map template is as below:

namespace std {
template<class Key, class T, class Pred, class A>
class map;

What are "class Pred" and "class A" for?

As a good rule of thumb never ever look at the standard headers.
They are not designed to be self documenting and may include extensions to
the standard which, if you use them, will make your code non-portable (In
particular I believe that implementations are allowed to add additional
defaulted template parameters).

This warning is particularly relevant to using the STL as alot of stuff is
not defined in the header at all such as what it means to be an acceptable
iterator to an STL algorithm - this is a particular problem of all
templates - they are largely defined by descriptions of behaviour rather
than code.

If you do the right thing and read the docs/manual pages then you will not
have to ask questions like this and you will write portable, standard
compliant C++.

If you are just poking around for curiosity's sake then I suggest you look
at http://www.dinkumware.com/refxcpp.html which gives the documentation for
dinkumwares C++ library implementation. This isn't a definition of the
standard but it is probably close enough for you.
When not to use STL at all?

If you're compiler is lousy at inlining then the STL generated code will be
truly awful (too many trivial function calls).
I had a conflict once where I was not supposed to use optimization and yet
the compiler would not inline enough without it. In the end I got a waiver
to turn optimization on.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top