Re: maps and constructors
Hicham Mouline wrote:
Hello,
I am attempting to design a class:
class C {
std::map<double, const A> ma;
std::map<double, const B> ma;
.
public:
C(...);
};
The keys in the 2 maps are always identical. So I could choose:
std::map<double, const std::pair<A,B> >
or
std::map<double, const std::pair<A,B>* >
(ps: AFAIK, using a const-ref for the pair is impossible, but
possible with some boost classes)
Q1:
Is there a difference in memory usage, access speed or code
readability between the top choice and bottom ones?
There is no "bottom one" to speak of. What does the pointer
point to? Without knowing that, it's impossible to tell you
the difference in memory usage, performance, or anything else.
Q2:
Between the 2 bottom ones (storing the pair object in the map vs
storing the ptr)
Same story. You don't explain where the pointer comes from, how
can we tell the difference?
Q3:
For users of class C, what sort of constructors should I offer?
C::C(const std::map<double, const std::pair<A,B>* >& )
You should offer what the users want. Ask the users. Since
you should synchronize the 'maps' it would be preferred to add
both 'A' and 'B' at the same time. Whether the users are going
to want to create a 'C' object from a pre-defined 'std::map',
I am not sure. Isn't the fact that you store those in a map
a simple implementation detail? It shouldn't dictate the user
interface to your class, you know.
Or, should I allow adding elements to the map after the object being
constructed?
C::add(double, const A&, const B&)
That seems preferable.
The users might also have arrays of 'double', 'A', and 'B', from
which they might want you to create your 'C' object. Consider
offering
C::C(size_t N, double* keys, const A* v1, const B* v2);
and tell them to make sure those arrays should better have at
least N elements. Then create your map in a loop, for example.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask