马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 bin554385863 于 2019-11-21 23:30 编辑
第149行和第156行的函数怎么能避免访问空串
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
#include <initializer_list>
#include <string>
template <typename tp>
class numbers
{
private:
size_t numsLen;
tp *numsPtr;
public:
//1--默认构造
numbers() : numsLen(0), numsPtr(nullptr) {}
//2--使用C风格数字数组构造
numbers(const tp *arr, const size_t arrSize);
//3--可变参数构造
numbers(std::initializer_list<tp> list);
//4--拷贝构造
numbers(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//5--按索引获取元素;按索引修改元素的值
tp &at(const size_t index) const;
//6--对象的大小
size_t const size() const;
/*--------------------------------------------------------*/
//7--重载下标运算符
tp &operator[](const size_t index) const;
//8--深度拷贝
numbers<tp> &operator=(const numbers<tp> &nums);
//9--重载流运算符<<
friend std::ostream &operator<<(std::ostream &os, const numbers<tp> &nums)
{
size_t sz = nums.size();
for (size_t i = 0; i < sz; i++)
{
os << nums[i] << " ";
}
return os;
}
/*--------------------------------------------------------*/
//10--添加元素
numbers<tp> operator+(const tp num) const;
numbers<tp>& operator+=(const tp num)
{
*this = *this + num;
return *this;
}
//11--合并对象
numbers<tp> operator+(const numbers<tp> &nums) const;
numbers<tp>& operator+=(const numbers<tp> &nums)
{
*this = *this + nums;
return *this;
}
/*--------------------------------------------------------*/
//12--判断数据在对象中存在
bool operator%(const tp num) const;
//13--判断子对象是否父对象的子串
bool operator%(const numbers<tp>& nums) const;
/*--------------------------------------------------------*/
//14--返回数据出现的次数
size_t operator/(tp num) const;
//15--返回两个对象共同的元素
numbers<tp> operator/(const numbers<tp> &nums) const;
//15-1返回并赋值
numbers<tp> operator/=(const numbers<tp> &nums)
{
*this = *this / nums;
return *this;
}
/*--------------------------------------------------------*/
//16--删除元素
numbers<tp> operator-(const tp num) const;
numbers<tp> operator-(const tp num)
{
*this = *this - num;
return *this;
}
//17--删除父对象中与子对象中重复的元素
numbers<tp> operator-(const numbers<tp> &nums) const;
numbers<tp> operator-(const numbers<tp> &nums)
{
*this = *this - nums;
return *this;
}
//18--按索引删除元素(数据必须显示声明size_t类型)失败返回空串
numbers<tp> operator-(const size_t index) const;
numbers<tp> operator-(const size_t index)
{
*this = *this - this->at(index);
return *this;
}
/*--------------------------------------------------------*/
~numbers()
{
delete[] numsPtr;
}
};
//2--使用C风格数字数组构造
template <typename tp>
numbers<tp>::numbers(const tp *arr, const size_t arrSize) : numsLen(arrSize)
{
numsPtr = new tp[numsLen];
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(arr + i);
}
}
//3--可变参数构造
template <typename tp>
numbers<tp>::numbers(std::initializer_list<tp> list) : numsLen(list.size())
{
numsPtr = new tp[numsLen];
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(list.begin() + i);
}
}
//4--拷贝构造
template <typename tp>
numbers<tp>::numbers(const numbers<tp> &nums)
{
if (nums.numsPtr != nullptr)
{
numsLen = nums.numsLen;
numsPtr = new tp[numsLen];
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
else
{
numsLen = 0;
numsPtr = nullptr;
}
}
/*--------------------------------------------------------------------*/
//5--按索引获取元素;按索引修改元素的值
template <typename tp>
inline tp &numbers<tp>::at(const size_t index) const
{
return numsPtr[index];
}
//6--对象的大小
template <typename tp>
inline const size_t numbers<tp>::size() const
{
return numsLen;
}
/*--------------------------------------------------------*/
//7--重载下标运算符
template <typename tp>
inline tp &numbers<tp>::operator[](const size_t index) const
{
return numsPtr[index];
}
/*--------------------------------------------------------*/
//8--深度拷贝
template <typename tp>
numbers<tp> &numbers<tp>::operator=(const numbers<tp> &nums)
{
if ((numsPtr != nullptr)&&(nums.numsPtr != nullptr))//非空串的深度拷贝
{
numsLen = nums.numsLen;
delete[] numsPtr;
numsPtr = new tp[numsLen];
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
if((numsPtr == nullptr)&&(nums.numsPtr != nullptr))//调用对象为空,操作对象非空
{
numsLen = nums.numsLen;
numsPtr = new tp[numsLen];
for (size_t i = 0; i < nums.numsLen; i++)
{
this->at(i) = *(nums.numsPtr + i);
}
}
if ((numsPtr != nullptr)&&(nums.numsPtr == nullptr))//调用对象非空,操作对象为空
{
numsLen = 0;
delete [] numsPtr;
numsPtr = nullptr;
}
return *this;
}
/*--------------------------------------------------------*/
//10--添加元素
template <typename tp>
numbers<tp> numbers<tp>::operator+(const tp num) const
{
numbers<tp> t_nums = *this;
if (t_nums.numsPtr != nullptr)
{
tp *t_numsPtr = new tp[numsLen + 1];
for (size_t i = 0; i < numsLen; i++)
{
*(t_numsPtr + i) = this->at(i);
}
*(t_numsPtr + numsLen) = num;
t_nums.numsLen++;
delete[] t_nums.numsPtr;
t_nums.numsPtr = t_numsPtr;
}
else
{
t_nums.numsLen+=1;
t_nums.numsPtr = new tp[numsLen]{num};
}
return t_nums;
}
//11--合并对象
template <typename tp>
numbers<tp> numbers<tp>::operator+(const numbers<tp> &nums) const
{
numbers<tp> t_nums;
if (numsPtr != nullptr)
{
t_nums = *this;
for (size_t i = 0; i < nums.numsLen; i++)
{
t_nums = t_nums + *(nums.numsPtr + i);
}
}
else
{
t_nums = nums;
}
return t_nums;
}
/*--------------------------------------------------------*/
//12--判断数据在对象中是否存在
template <typename tp>
bool numbers<tp>::operator%(const tp num) const
{
bool flag = false;
if (numsPtr != nullptr)
{
for (size_t i = 0; i < numsLen; i++)
{
if (num == *(numsPtr + i))
{
flag = true;
break;
}
}
}
return flag;
}
//13--判断子对象是否父对象的子串
template <typename tp>
bool numbers<tp>::operator%(const numbers<tp> &nums) const
{
bool flag = false;
size_t count = 1;
if (numsLen > nums.numsLen)
{
for (size_t i = 0; i < nums.numsLen; i++)
{
if ((*this) % (*(nums.numsPtr + i)))
{
count++;
if (count == nums.numsLen)
{
break;
}
}
}
if (count == nums.numsLen)
{
flag = true;
}
}
if (nums.numsPtr == 0)
{
flag = true;
}
return flag;
}
/*--------------------------------------------------------*/
//14--返回数据出现的次数
template <typename tp>
size_t numbers<tp>::operator/(tp num) const
{
size_t count = 0;
if (numsPtr != nullptr)
{
for (size_t i = 0; i < numsLen; i++)
{
if (this->at(i) == num)
{
count++;
}
}
}
return count;
}
//15--返回两个对象共同的元素
template <typename tp>
numbers<tp> numbers<tp>::operator/(const numbers<tp> &nums) const
{
numbers<tp> r_nums;
if ((numsPtr != nullptr)&&(nums.numsPtr != nullptr))
{
for (size_t i = 0; i < nums.numsLen; i++)
{
if ((*this)% (*(nums.numsPtr + i)))
{
r_nums = r_nums + *(nums.numsPtr + i);
}
}
}
return r_nums;
}
/*--------------------------------------------------------*/
//16--删除元素
template <typename tp>
numbers<tp> numbers<tp>::operator-(const tp num) const
{
numbers<tp> r_nums;
if (numsPtr != nullptr)
{
for (size_t i = 0; i < numsLen; i++)
{
if (this->at(i) != num)
{
r_nums = r_nums + *(numsPtr + i);
}
}
}
return r_nums;
}
//17--删除父对象中与子对象中重复的元素
template <typename tp>
numbers<tp> numbers<tp>::operator-(const numbers<tp>& nums) const
{
numbers<tp> r_nums;
size_t count = 0;
if ((numsPtr != nullptr)&&(nums.numsPtr != nullptr))
/*两个非空数字串*/
{
r_nums = *this;
for (size_t i = 0; i < nums.numsLen; i++)
{
r_nums = r_nums - *(nums.numsPtr + i);
}
if (r_nums.numsLen == 0)
{
delete [] r_nums.numsPtr;
r_nums.numsPtr = nullptr;
}
}
else if ((numsPtr != nullptr)&&(nums.numsPtr == nullptr))
/*子串为空*/
{
r_nums = *this;
}
return r_nums;
}
//18--按索引删除元素(数据必须是size_t类型)失败返回空串
template <typename tp>
numbers<tp> numbers<tp>::operator-(const size_t index) const
{
numbers<tp> r_nums;
if ((index < numsLen ) && (numsPtr != nullptr))
{
for (size_t i = 0; i < index; i++)
{
r_nums = r_nums + *(numsPtr + i);
}
for (size_t i = index + 1; i < numsLen; i++)
{
r_nums = r_nums + *(numsPtr + i);
}
}
return r_nums;
}
#endif
|