Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a custom allocator for strings

I know I can set a custom allocator for vectors using the syntax vector<T, Alloc>. Is there a way I can do the same for strings?

like image 915
Vlad the Impala Avatar asked Feb 04 '23 03:02

Vlad the Impala


1 Answers

Yes. All string classes come from the class template basic_string, declared as such:

template <class charT, class traits = char_traits<charT>,
            class Allocator = allocator<charT> >
class basic_string;

For example, std::string is just typedef basic_string<char> string;.

The third template parameter is the allocator, so you can do something like:

typedef basic_string<char, char_traits<char>, my_allocator<char> > my_string;
like image 113
GManNickG Avatar answered Feb 05 '23 16:02

GManNickG