2021-09-07


提示:以下是本篇文章正文内容,下面案例可供参考

一、xxx

二、编程题

1.写一个string类。

代码如下(示例):

#include<iostream>
#include<string.h>//strcpy和strlen函数的头文件
using namespace std;
class MyString {
public:
	//构造函数
	MyString(const char * str=nullptr);
	//析构函数
	~MyString();
	//拷贝构造函数
	MyString(const MyString & s);
	//赋值
	MyString operator=(const MyString &other);
	//获取字符串
	char* getChar();
	
private:
	char* m_data;

};
//构造函数
MyString::MyString(const char* str)
{
	//判断 str是否为空
	if (str)
	{
		//str不为空,我们需要把str的内容赋给m_data
		m_data = new char[strlen(str)+1];	//加1,是因为char后面默认最后一位为\0
		//strcpy()和strcpy_s,前面两个参数,后面三个参数。strcpy_s三个参数,第一个被赋值的对象,第二个表示长度,第三个表示赋值对象
		strcpy_s(m_data,strlen(str)+1,str);
	}
	else
	{
		//str 为空,说明没有传入值
		m_data = new char[1];
		*m_data = '\0';
	}
}
//析构函数
MyString::~MyString()
{
	delete[]m_data;
}
//拷贝构造函数
MyString::MyString(const MyString& s)
{
	//把mystring对象复制给当前对象
	m_data = new char[strlen(s.m_data)+1];
	strcpy_s(m_data,strlen(s.m_data)+1,s.m_data);
}
//赋值运算符
MyString MyString::operator=(const MyString& other)
{
	//把字符串赋值给当前对象
	if (this == &other)
	{
		//返回当前对象
		return *this;
	}
	delete[] m_data;
	m_data = new char[strlen(other.m_data)+1];
	strcpy_s(m_data,strlen(other.m_data)+1,other.m_data);
	return *this;
}
char* MyString::getChar()
{
	return this->m_data;
}
int main()
{
	MyString s1("12231");
	 MyString s2 = s1;
	 MyString s3(s2);
	 cout << s3.getChar()<<endl;
	 system("pause");
	return 0;
}

2.

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值