string 不能使用memset or memcpy

 

string 不能使用memset或者memcpy 这些低级函数。

 

 



string类是C++中专门处理字符串的类,它的实际上是basic_string<char>的一个typedef。它有四个跌代器:
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef _String_iterator<_Elem, _Traits, _Alloc> iterator;//models random iterator
typedef _String_const_iterator<_Elem, _Traits, _Alloc> const_iterator;models random iterator//models random iterator
一个静态常量:static const size_typed npos = -1;
basic_string模板定义的类型:
typedef    traits                                                         traits_type;        //某个具体类型的模板参数                        
typedef    typename traits::char_type                         value_type;       
typedef   Allocator                                                   allocator_type;
typedef   typename Allocator::size_type                    size_type;
typedef   typename Allocator::difference_type           difference_type; 
typedef   typename Allocator::reference                    reference;
typedef   typename Allocator::const_reference          const_reference;
typedef   typename Allocator::pointer                        pointer;
typedef   typename Allocator::const_pointer              const_pointer;string类是C++中专门处理字符串的类,它的实际上是basic_string<char>的一个typedef。它有四个跌代器:
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef _String_iterator<_Elem, _Traits, _Alloc> iterator;//models random iterator
typedef _String_const_iterator<_Elem, _Traits, _Alloc> const_iterator;models random iterator//models random iterator
一个静态常量:static const size_typed npos = -1;
basic_string模板定义的类型:
typedef    traits                                                         traits_type;        //某个具体类型的模板参数                        
typedef    typename traits::char_type                         value_type;       
typedef   Allocator                                                   allocator_type;
typedef   typename Allocator::size_type                    size_type;
typedef   typename Allocator::difference_type           difference_type; 
typedef   typename Allocator::reference                    reference;
typedef   typename Allocator::const_reference          const_reference;
typedef   typename Allocator::pointer                        pointer;
typedef   typename Allocator::const_pointer              const_pointer;

 

 

 

下面是 memsey 和memcpy的源码

 

/*******************************************************
 *  memcpy and memset
 *  the same as copy
 *******************************************************/
void * Mymemcpy(void *desStr, const void *srcStr, int n) {    //内存拷贝
    char *s1 = (char *)desStr;
    char *s2 = (char *)srcStr;
    while(n --)
        *s1 ++ = *s2 ++;
    return desStr;
}
void * Mymemset(void *str, char c, int n){        //区域赋值
    char *s = (char *)str;
    while(n --){
        *s ++ = c;
    }
    return str;
}

 

 

对于String来说,我们在编辑器加监视,你会发现内部除了字符还有别的东西,所以

new完后string的构造函数已经调用完毕,你再memset就破坏了他的内部数据不变式。

 

 

 

在网上看到这样一条评论,觉得有道理:
任何类都不能用memset, 一旦暴力,就等于你强奸了她的内部数据,她已经崩溃了

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小鱼酱

用心写好每一篇博客

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值