C++实现字符串mystring类的封装(难点:赋值运算符函数)

2020年11月10日 周二 天气晴 【不悲叹过去,不荒废现在,不惧怕未来】



1. 引言

用C++实现了简单的字符串类封装,实现了字符串赋值、相加、修改、比较等功能。完整工程放在了我的github上,有需要的也可以自取。

github地址:https://github.com/March225/High-frequency-algorithm

2. 主文件——main.cpp

/**
 *  @Copyright (C) 2020 March. All rights reserved.
 *  @license   GNU General Public License (GPL)
 *  @author	   March
 *  @email	   345916208@qq.com
 *  @file	   main.cpp
 *  @brief	   字符串类封装主文件
 *  @version   1.0
 *  @date	   2020-11-05
 */
#include <iostream>
#include "mystring.h"
using namespace std;

void test01() {
   
	MyString str = "德玛西亚!";

	// 键盘输入字符串再输出
	cin >> str;
	cout << "str:" << str << endl;

	// 拷贝字符串
	MyString str2 = str;
	cout << "str2:" << str2 << endl;
}

void test02() {
   
	MyString str = "hyhyh";
	MyString str2("");

	// 字符串赋值
	str2 = "yyyyyy";
	cout << "str2:" << str2 << endl;

	// 字符串修改
	str2[0] = '0';
	cout << "str2:" << str2 << endl;

	// 字符串相加
	MyString str3 = "-*+++";
	MyString str4 = "-*+++";
	MyString str5 = str3 + "+777";
	cout << "str5:" << str5 << endl;

	// 字符串比较
	if(str3==str4) cout << "str3 == str4"<< endl;
	else cout << "str3 != str4" << endl;

	if (str3 == "+++") cout << "str3 == +++" << endl;
	else cout << "str3 != +++" << endl;
}

int main() {
   
	//test01();
	test02();
	return 0;
}

3. mystring.h

#pragma once
#include <cstring>
#include <iostream>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值