Setting a custom allocator for strings

。_饼干妹妹 提交于 2019-12-04 08:35:42

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/3268117/setting-a-custom-allocator-for-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!