1

I am thinking of how to pass allocator object to STL C++ containers, for example as below

std::allocator<int> intAlloc; vector<int, intAlloc> v1; 

I do aware of we can do like below

vector<int, std::allocator<int> > v2; 

I am think of options of how to pass a same allocator object to all vector objects instead of passing allocator type.

1
  • Do you want std::vector<int, std::allocator<int> > v2(intAlloc); ? Commented Dec 11, 2017 at 13:13

2 Answers 2

1

As mentioned in comment by Jarod above. I am looking for some thing below which can be achieved by using same allocator object for both vectors.

std::allocator<int> intAlloc; vector<int, std::allocator<int> > v2(intAlloc); vector<int, std::allocator<int> > v2(intAlloc); 
Sign up to request clarification or add additional context in comments.

Comments

0

The allocator is shared between all objects of this type:

Every standard library component that may need to allocate or release storage, from std::string, std::vector, and every container except std::array, to std::shared_ptr and std::function, does so through an Allocator: an object of a class

It is, in other words, impossible to not share the allocator between objects.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.