c语言实现图像resize函数,C标准向量resize()函数

在某些实现中,调整大小几乎肯定会首先调用reserve.最近实现了std :: vector的变体,下面是

std::vector::reserve的精简和注释版本……(这些评论是为了OP的理解)……实际上,大多数STL实现将比这稍微复杂一些(对于调试目的);但它的概念基本相同.

template

void Vector::reserve(SizeType sz){

//if only the new size is greater than current capacity

if(sz > m_capacity){

//allocate the new size

T* data = static_cast(SFAllocator::allocate(sz));

//move all previous data

for(SizeType i=0; i < m_size; i++){

new(data+i) T(std::move(m_data[i])); //! TODO: move if only noexcept;

//call the destructor on the moved item

call_destructor(m_data[i]);

}

//deallocate formerly used memory

SFAllocator::deallocate(m_data);

//reassign the capacity to the new capacity

m_capacity = sz;

m_data = data; //reassign the data pointer

//size remains the same

}

以下是std::vector::resize的精简和注释版本.正如您在下面看到的那样,调整大小会先调用预留.

template

void Vector::resize(SizeType sz){

// resize it to fit at least fit to "sz"

reserve(sz);

//if higher size is requested

if(sz > m_size)

//default construct the remainder of the new uninitialized memory

for(SizeType i= m_size; i < sz; i++)

new(m_data+i) T{}

//if the container size is to get smaller

else

for(SizeType i=sz; i

call_destructor(m_data[i]); //delete the elements at indexes above "sz"

m_size = sz; //change container size.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值