a few stl container questions: allocators and comparers.

S

sheam

I am using a custom allocator with std::map. Is there anyway to access
the allocator instance that belongs to my instance of map?

i.e.,

std::map<int,int,MyIntComparer,MyCustomAllocator> m;
.....
.....
.....
m.allocator()->printUsageStats();

Also, I don't really need a specialized comparerer, but I had to provide
on, in order to use MyCustomAllocator. Is there a way to specify an
allocator, but not a comparator?

Thanks,
~S
 
S

sheam

sheam said:
Also, I don't really need a specialized comparerer, but I had to provide
on, in order to use MyCustomAllocator. Is there a way to specify an
allocator, but not a comparator?

Got it, std::less<const int> did it.

~S
 
S

sheam

sheam said:
std::map<int,int,MyIntComparer,MyCustomAllocator> m;
....
....
....
m.allocator()->printUsageStats();

Ok, it looks like there is a "get_allocator() const" function, but it
returns a copy of the allocator. So doing something like this would not
work:

enum Pool { PoolA, PoolB, PoolC };

m.get_allocator().SetPoolID( PoolB );

I guess, I will have to do it with a template parameter for teh custom
allocator:

std::map< int, int, std::less<const int>, MyCustomAllocator<PoolB> > m;

If anyone has a better idea, let me know.

~S
 
S

sheam

sheam wrote:
Ok one last question on using my custom allocator with std::map:

should the allocator's value_type template argument be of type
pair<mykey,myval> or just myval type? Both seem to compile and run fine.

i.e.,

map< int,
Test,
less<const int>,
CustomAllocator<Test> > my_map;

or

map< int,
Test,
less<const int>,
CustomAllocator<pair<const int,Test> > > my_map;


Is the 'rebind' struct making them functionally equivilant?

Thanks.

~S
 
S

sheam

sheam said:
sheam wrote:
Ok one last question on using my custom allocator with std::map:

should the allocator's value_type template argument be of type
pair<mykey,myval> or just myval type? Both seem to compile and run fine.

i.e.,

map< int,
Test,
less<const int>,
CustomAllocator<Test> > my_map;

or

map< int,
Test,
less<const int>,
CustomAllocator<pair<const int,Test> > > my_map;


Is the 'rebind' struct making them functionally equivilant?

Thanks.

~S


In fact, I get same results if I do this:
map< int,
Test,
less<const int>,
CustomAllocator<float> > > my_map;

????

~S
 
K

Kai-Uwe Bux

This would be the usual choice.

In fact, I get same results if I do this:
map< int,
Test,
less<const int>,
CustomAllocator<float> > > my_map;

The default allocator type is std::alloctor< value_type >. Internally,
std::map has to use nodes that are not of type value_type. Therefore, it ha
to use a rebind trick to get the right allocator type. That is the reason
why it could compile with float.

However, the standard says that

std::map< Key, T, C, A >::reference

is a typedef for A::reference. The same goes for pointer, const_reference,
and const_pointer. As soon as you use these, the first and last version
should fail.


Best

Kai-Uwe Bux
 
S

Shea Martin

Kai-Uwe Bux said:
However, the standard says that

std::map< Key, T, C, A >::reference

is a typedef for A::reference. The same goes for pointer, const_reference,
and const_pointer. As soon as you use these, the first and last version
should fail.

Thanks. I noticed that it was essentially resulting an allocation of
std::pair, so that is what I was using.

~S
 
F

Frank Birbacher

Hi!
Ok, it looks like there is a "get_allocator() const" function, but it
returns a copy of the allocator. So doing something like this would not
work: [snip]
If anyone has a better idea, let me know.

Make the allocator stateless like a functor.

struct MyAllocState
{
Pool pool; //your enum
};

template<typename T>
struct MyAlloc
{
const shared_ptr<MyAllocState> mystate;
//...
};

use whatever pointer you like and have the state be shared by all copies
of a MyAlloc. That way you can change the state through the
get_allocator() function of a map.

The state could be helb by shared_ptr. Or by regular pointer if you
maintain an instance of the state somewhere else.

Frank
 

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

template with STL container 10
A Better Container Choice? 3
The perfect STL container 3
Containers & Allocators 6
Building a Large Container 26
STL Container Choice 11
Sorting an STL map 1
Help with STL allocators 6

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top