当前位置:网站首页>STL string input / output overload

STL string input / output overload

2022-07-19 11:50:00 joker_ 0030

1、Mystring.app(string Function function implementation )

#include "Mystring.h"
    stu::stu()
    {
        m_str = new char('\n');
        m_l = 0;
    }
    stu::stu(size_t length, char ch)
    {
        m_str = new char[length+1];
        for (size_t i = 0; i < length; i++)
        {
            m_str[i] = ch;
        }
        m_str[length] = '\0';
        m_l = length ;
    }
    stu::stu(const char* str)
    {
        const char* pStr = str;
        int n = 0;
        while ('\0' != *pStr)// Get the length
        {
            n++;
            pStr++;
        }
        m_str = new char[n+1];// To apply for space
        for (int i = 0; i < n+1; i++)// Space assignment
        {
            m_str[i] = str[i];
        }
        m_l = n ;
    }
    stu::stu(const char* str,size_t length)
    {
        if (NULL == str || length < 0)
            return;
        m_str = new char[length + 1];// To apply for space
        for (int i = 0; i < length; i++)// Space assignment
        {
            m_str[i] = str[i];
        }
        m_str[length] = '\0';
        m_l = length;

    }
    stu::stu(stu& str, size_t index, size_t length)
    {
        m_str = new char[length - index + 1];
        for (size_t i = index,j=0; i < length; i++,j++)
     {
            m_str[j] =str.c_str()[i];

      }
        m_str[length - index] = '\0';
        m_l = length-index;
    }

    stu::stu(const stu& str)
    {
        m_str = new char[str.size()+1];
        for (size_t i = 0; i < str.size() + 1; i++)
        {
            m_str[i] = str.c_str()[i];
        }
    }
    const  char*  stu::c_str() const
    {
        return m_str;
    }
    const size_t stu::size() const
    {
        return m_l;
    }
    ostream& operator <<(ostream& os, stu& str)
    {
        //os << str.c_str();
        os << str.m_str;
        return os;
    }
    istream& operator >>(istream& is, stu& str)
    {   // Input string
        char arr[20] = { 0 };
        is>>arr;
        // Calculate string length
        int i = 0;
        for (i = 0; arr[i] != '\0'; i++);
        // Release space
        //delete[] str.c_str();
        delete[] str.m_str;
        // To apply for space
        //str.GetMstr() = new char[i+1];
        str.m_str = new char[i+1];
        // assignment
        int j = 0;
        for (j = 0; j <= i; j++)
        {
            //str.GetMstr()[j] = arr[j];
            str.m_str[j] = arr[j];
        }
        // Number of characters changed
        //str.s_l(i);
        str.m_l=i;
        return is;
    }
   // Set the string content
    char* &stu::GetMstr()
    {
        return m_str;
    }
    // Set the number of strings
    void stu::s_l(size_t length)
    {
         m_l=length;
    }

    stu::~stu()
    {
        if (NULL != m_str)
        {
            delete[] m_str;
        }
    }
 

原网站

版权声明
本文为[joker_ 0030]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171512483251.html