动态数组的容器类bag

头文件bag2.h:

#ifndef MAIN_WTF_BAG2_H
#define MAIN_WTF_BAG2_H
#include  <cstdlib>


namespace main_wtf_4
{
class bag
{
public:
typedef int value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;


bag(size_type initial_capacity = DEFAULT_CAPACITY);
bag(const bag& source);
~bag();


void reserve(size_type new_capacity);


size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
void operator = (const bag& source);


size_type size() const { return used; };
size_type count(const value_type& target) const;
value_type getData(size_type i)  const { return data[i]; };



private:
value_type *data;
size_type used;
size_type capacity;


};
bag operator +(const bag& b1,const bag& b2);
}


#endif


实现文件bag2.cpp:

#include <algorithm>
#include<iostream>
#include <cassert>
#include "bag2.h"
using namespace std;


namespace main_wtf_4
{
// const bag::size_type bag::CAPACITY;


bag::bag(size_type initial_capacity)
{
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
}


bag::~bag()
{
delete[] data;
}


bag::bag(const bag& source)
{
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
copy(source.data,source.data+used,data);


}


void bag::reserve(size_type new_capacity)
{
value_type *larger_array;


if(new_capacity == capacity)
return;


if(new_capacity < used)
new_capacity = used;


larger_array = new value_type[new_capacity];
copy(data,data+used,larger_array);
delete[] data;
data = larger_array;
capacity = new_capacity;
}


bag::size_type bag::erase(const value_type& target)
{
size_type num = 0;


while(erase_one(target))
{
num++;


}
return num;

}


bool bag::erase_one(const value_type& target)
{
for(std::size_t i=0;i<used;i++)
{
if(target == data[i])
{
data[i] = data[used-1];
data[used-1] = 0;
used--;
return true;
}
}
return false;
}


void bag::insert(const value_type& entry)
{
if(used == capacity)
{
reserve(used+1);
}

data[used++] = entry;

}


bag::size_type bag::count(const value_type& target) const
{
size_type num = 0;
for(size_t i=0;i<used;i++)
{
if(target == data[i])
num++;
}
return num;
}


void bag::operator+=(const bag& addend)
{
if((used+addend.size()) > capacity)
{
reserve(used+addend.size());
}
copy(addend.data,addend.data+addend.used,data+used);
used += addend.size();
/*
else
{
for(size_t i=0;i<addend.size();i++)
{
data[used++] = addend.getData(i);


}
}*/
}


void bag::operator=(const bag& source)
{
value_type *new_data;


if(this == &source)
return;


if(capacity != source.capacity)
{
new_data = new value_type[source.capacity];
delete data;
data = new_data;
capacity = source.capacity;
}


used = source.used;
copy(source.data,source.data+used,data);
}


bag operator +(const bag& b1,const bag& b2)
{
bag bag1(b1.size()+b2.size());

bag1 += b1;
bag1 += b2; 


return bag1;

}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值