Setting a custom allocator for strings

前端 未结 1 745
半阙折子戏
半阙折子戏 2021-02-04 14:04

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

相关标签:
1条回答
  • 2021-02-04 14:38

    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;
    
    0 讨论(0)
提交回复
热议问题