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>