C语言assign方法,C++标准库assign()

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

zhzcxl

2014.02.25

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:48%    等级:12

已帮助:4448人

不知道你是用那个工具,我用VC 2003运行结果跟你说的不一样;代码:vector ivec1(100);

vector ivec2(10, 10);

vector:: const_iterator it = ivec1.begin();

ivec1 = ivec2;

cout<

cout<

{ // assign _Right

if (this != &_Right)

{ // worth doing

if (_Right.size() == 0)

clear(); // new sequence empty, free storage

else if (_Right.size() <= size())

{ // enough elements, copy new and destroy old

pointer _Ptr = copy(_Right._Myfirst, _Right._Mylast,

_Myfirst); // copy new

_Destroy(_Ptr, _Mylast); // destroy old

_Mylast = _Myfirst + _Right.size();

}

else if (_Right.size() <= capacity())

{ // enough room, copy and construct new

pointer _Ptr = _Right._Myfirst + size();

copy(_Right._Myfirst, _Ptr, _Myfirst);

_Mylast = _Ucopy(_Ptr, _Right._Mylast, _Mylast);

}

else

{ // not enough room, allocate new array and construct new

if (_Myfirst != 0)

{ // discard old array

_Destroy(_Myfirst, _Mylast);

this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);

}

if (_Buy(_Right.size()))

_Mylast = _Ucopy(_Right._Myfirst, _Right._Mylast,

_Myfirst);

}

}

return (*this);

}{ // enough elements, copy new and destroy old

pointer _Ptr = copy(_Right._Myfirst, _Right._Mylast,

_Myfirst); // copy new

_Destroy(_Ptr, _Mylast); // destroy old///只释放了多余的空间

_Mylast = _Myfirst + _Right.size();

}

00分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值