C++ day05

作业:

头文件:

#ifndef MY_STRING_H
#define MY_STRING_H
#include <cstring>
class My_string
{
private:
    char *ptr;         //指向字符数组的指针
    int size;           //字符串的最大容量
    int len;            //字符串当前容量
public:
    //无参构造
    My_string();
    //有参构造
    My_string(const char* src);
    My_string(int num, char value);
    //拷贝构造
    My_string(const My_string & other);
    //拷贝赋值
    My_string& operator=(const My_string & other);
    //析构函数
    ~My_string();
    //判空
    bool empty();
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();
    //二倍扩容
    void my_resize(int new_size);
    //算数运算符+
    My_string operator+(const My_string &R);

};


#endif // MY_STRING_H

源文件:

#include <iostream>
#include "MY_string.h"
using namespace std;

My_string::My_string():size(15)
{
    this->ptr = new char[size];
    this->ptr[0] = '\0';            //表示串为空串
    this->len = 0;
}
//有参构造
My_string::My_string(const char* src)
{
    size = strlen(src);
    ptr = new char[size +1];
    strcpy(ptr,src);
}
//有参构造
My_string::My_string(int num, char value)
{
    size=num+1;  //+1留出终止符
    len=num;       //实际长度
    ptr=new char[size];
    for(int i=0;i<len;i++)
    {
        ptr[i]=value;
    }
    ptr[len]='\0';
}
//拷贝构造
My_string::My_string(const My_string & other)
{
    this->size=other.size;
    this->len=other.len;
    this->ptr=new char[this->size];
    strcpy(ptr,other.ptr);
}
//拷贝赋值
My_string& My_string::operator=(const My_string & other)
{
    if(this!=&other)
    {
        delete[]ptr; //释放原有内存
        this->size=other.size;
        this->len=other.len;
        this->ptr=new char[this->size];
        strcpy(this->ptr,other.ptr);
    }
    return *this;
}
//析构函数
My_string::~My_string()
{
    delete []this->ptr;
}
//判空
bool My_string::empty()
{
    return this->len==0;
}
//尾插
void My_string::push_back(char value)
{
       if (len + 1 >= size)
            {
                // 扩容
                my_resize(size * 2);
            }
        ptr[len++]=value;  //加入字符
        ptr[len]='\0';
 }
//尾删
void My_string::pop_back()
{
     if(len>0)  //判断是否可以删除
     {
         len--;   //长度-1
         ptr[len]='\0';
     }
}
//at函数实现
char My_string::at(int index)
{
    if(index>=0 && index<size)
    {
        return ptr[index];
    }
    else
    {
        cout<<"超出范围"<<endl;
        exit(1);
    }
}
//清空函数
void My_string::clear()
{
    this->len = 0;
    ptr[0] = '\0';
}
//返回C风格字符串
char *My_string::data()
{
    return this->ptr;
}
//返回实际长度
int My_string::get_length()
{
    return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
    return this->size;
}
//君子函数:二倍扩容
void My_string::my_resize(int new_size)
{
     char* new_ptr = new char[new_size]; // 动态分配新数组
     strcpy(new_ptr, ptr); // 复制旧数据
     delete[] ptr; // 释放旧内存
     ptr = new_ptr; // 更新指针
     size = new_size; // 更新大小
}
//算术运算符+
My_string My_string::operator+(const My_string &R)
{
    this->size=R.size;
    this->len=R.len;
    delete []this->size;
    this->ptr=new char[this->size];
    strcpy(ptr,R.ptr);
    return this->ptr;
}

主函数:

#include <iostream>
#include <cstring>
#include "MY_string.h"
using namespace std;

int main()
{
    My_string str("hello");
    My_string s1("nihao");
    My_string s2("zaijian");
    str.push_back('w'); // 尾插字符
    std::cout << "输出为: " << str.data() << std::endl;
    std::cout << "实际长度: " << str.get_length() << std::endl; 
    std::cout << "总长度: " << str.get_size() << std::endl;     
    str.pop_back(); // 尾删字符
    std::cout << "删除后的数据为: " << str.data() << std::endl; 
    str.clear(); // 清空字符串
    std::cout << "清空后的内容为 " << str.data() << std::endl; 
   
    return 0;
}

C++ 标准库中,并没有名为 `day_iterator` 的参数或类型。可能用户提到的 `day_iterator` 是对某些迭代器(iterator)概念的一种误解,或者是特定库中的自定义类型。 在标准库中,与日期相关的功能主要出现在 `<chrono>` 头文件中,而 C++20 引入了更完整的日期和时间处理功能,包括 `std::chrono::year_month_day` 和相关的日期操作。然而,C++ 标准库并未提供专门的 `day_iterator` 类型用于遍历日期。 如果需要实现类似功能,可以自定义一个日期迭代器,例如使用 `std::chrono` 中的日期操作来实现逐日递增的迭代器[^1]。 以下是一个简单的示例,展示如何使用 C++20 的 `<chrono>` 库来实现一个“按天递增”的迭代器: ```cpp #include <iostream> #include <chrono> namespace chrono = std::chrono; class day_iterator { private: chrono::sys_days _current; public: using iterator_category = std::forward_iterator_tag; using value_type = chrono::sys_days; using difference_type = std::ptrdiff_t; using pointer = const chrono::sys_days*; using reference = const chrono::sys_days&; explicit day_iterator(chrono::sys_days start) : _current(start) {} reference operator*() const { return _current; } pointer operator->() const { return &_current; } day_iterator& operator++() { _current += chrono::days(1); return *this; } day_iterator operator++(int) { day_iterator tmp = *this; ++(*this); return tmp; } bool operator==(const day_iterator& other) const { return _current == other._current; } bool operator!=(const day_iterator& other) const { return !(*this == other); } }; int main() { // 从 2023-10-01 开始 auto start_date = chrono::sys_days{chrono::year(2023)/10/1}; auto end_date = chrono::sys_days{chrono::year(2023)/10/6}; day_iterator begin(start_date); day_iterator end(end_date); for (auto it = begin; it != end; ++it) { auto ymd = chrono::year_month_day(*it); std::cout << static_cast<std::string>(ymd) << std::endl; } return 0; } ``` 该程序输出如下内容: ``` 2023-10-01 2023-10-02 2023-10-03 2023-10-04 2023-10-05 ``` 上述代码展示了如何创建一个前向迭代器,用于遍历从起始日期到结束日期之间的每一天。这个自定义的 `day_iterator` 实现了基本的迭代器接口,支持前置递增、解引用和比较操作。 ### 示例说明 - 使用 `std::chrono::sys_days` 表示系统时钟上的日期。 - 每次调用 `operator++()` 都会将当前日期增加一天。 - 迭代器支持与标准算法兼容的接口,例如 `begin()` 和 `end()`。 ### 注意事项 - 该实现是前向迭代器(`std::forward_iterator_tag`),不支持双向或随机访问。 - 如果未使用 C++20 或 `<chrono>` 的日历功能,可能需要依赖第三方库如 Boost.Date_Time 来实现类似的日期迭代功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值