当前位置:网站首页>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;
}
}
边栏推荐
- 2022.07.13 summer training personal qualifying (VIII)
- SQL UNION操作符
- QT -- excellent open source project
- Docker安装MySQL
- 【机器学习】多标签分类的评价指标与代码实现
- 02-3、指针和引用的区别
- 传输层 -------- TCP(一)
- QT learning diary 17 - QT database
- Wechat applet cloud development 1 - Database
- Total number of blocking and waiting in jconsole thread panel (RPM)
猜你喜欢

LeetCode_17_电话号码的字母组合

《MySQL DBA封神打怪之路》专栏学习大纲

Transport layer -------- TCP (I)

TCP congestion control details | 7 Surpass TCP

Introduction of database lock, shared with InnoDB, exclusive lock

Opencv draw a black rectangle and write the serial number

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning

Leetcode 1328. Destroy palindrome string (yes, solved)

Send blocking, receive blocking
![[wechat applet] use a thousand hand float - rollback](/img/52/4939ff2a644c46fdb388a64ac111c6.png)
[wechat applet] use a thousand hand float - rollback
随机推荐
STL string输入输出重载2
A curated list of awesome Qt and QML
To build agile teams, these methods are indispensable
Hot discussion: my husband is 34 years old this year and wants to read a doctoral degree. What should I do in the future to do scientific research?
03-1、内联函数、auto关键字、typeid、nullptr
The difference between CPU load and CPU utilization
JVM hook hooks function
02-2、缺省参数、函数重载、引用、隐式类型转换、关于报错
学习笔记3--规划控制中的机器学习基本思想
Fundamentals of scala (3): operators and process control
搭建OpenStack-M版的Cinder所碰到过的状况
TiDB 内存控制文档
解决邮件客户端QQ Mail及Thunderbird无法登入Outlook的问题
SQL UNION操作符
Leetcode 1304. 和为零的 N 个不同整数
Choice is more important than effort
Leetcode 1310. Subarray XOR query
Performance optimization @contented to reduce pseudo sharing
梦想CMS 前台搜索SQL注入
Solve the problem that QQ mail and Thunderbird cannot log in to outlook