为字符串设置自定义分配器 [英] Setting a custom allocator for strings

查看:170
本文介绍了为字符串设置自定义分配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用语法 vector< T,Alloc> 为向量设置自定义分配器。有没有办法我可以做同样的字符串?

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?

推荐答案

是的。所有字符串类都来自类模板 basic_string ,声明如下:

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;

例如, std :: string 只需 typedef basic_string< char> string;

第三个模板参数是allocator,所以你可以这样做:

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;

这篇关于为字符串设置自定义分配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆