parent
							
								
									e22d05f823
								
							
						
					
					
						commit
						847ec0fb15
					
				@ -0,0 +1,6 @@ | 
				
			||||
cmake_minimum_required(VERSION 3.21) | 
				
			||||
project(work20220407) | 
				
			||||
 | 
				
			||||
set(CMAKE_CXX_STANDARD 14) | 
				
			||||
 | 
				
			||||
add_executable(work20220407 main.cpp string/String.cpp string/String.h) | 
				
			||||
@ -0,0 +1,26 @@ | 
				
			||||
#include <iostream> | 
				
			||||
#include "string/String.h" | 
				
			||||
 | 
				
			||||
 | 
				
			||||
using namespace std; | 
				
			||||
 | 
				
			||||
int main() { | 
				
			||||
    String s1("Help!"),s2("Good!"),s3(s2),s4,s5; | 
				
			||||
    cout<<"s1="<<s1<<endl; | 
				
			||||
    s3="Hello!"; | 
				
			||||
    cout<<"s3="<<s3<<endl; | 
				
			||||
    s3=s2; | 
				
			||||
    cout<<"s3="<<s3<<endl; | 
				
			||||
    s3+=s2; | 
				
			||||
    cout<<"s3="<<s3<<endl; | 
				
			||||
    cin>>s4; | 
				
			||||
    cout<<"s4="<<s4<<endl; | 
				
			||||
    cout<<"s4length="<<s4.Length()<<endl; | 
				
			||||
 | 
				
			||||
    /*s5=s3+s4;
 | 
				
			||||
    cout<<"s5="<<s5<<endl; | 
				
			||||
    s5[0]='g'; | 
				
			||||
    cout<<"s5="<<s5<<endl; | 
				
			||||
    cout<<"strlen(s5)="<<s5.Length()<<endl;*/ | 
				
			||||
    cout<<boolalpha<<(s3 == s1)<<endl; | 
				
			||||
} | 
				
			||||
@ -0,0 +1,71 @@ | 
				
			||||
//
 | 
				
			||||
// Created by lenfrex on 2022/4/4.
 | 
				
			||||
//
 | 
				
			||||
 | 
				
			||||
#ifndef WORK20220401_STRING_H | 
				
			||||
#define WORK20220401_STRING_H | 
				
			||||
 | 
				
			||||
#include <iostream> | 
				
			||||
#include <string> | 
				
			||||
#include "utility" | 
				
			||||
 | 
				
			||||
using namespace std::rel_ops; | 
				
			||||
 | 
				
			||||
class String { | 
				
			||||
private: | 
				
			||||
    // 字符数组做字符串
 | 
				
			||||
    char *data = nullptr; | 
				
			||||
 | 
				
			||||
    // 字符串实际长度
 | 
				
			||||
    int length = 0; | 
				
			||||
 | 
				
			||||
    // 已分配的字符数量
 | 
				
			||||
    int allocSize = 0; | 
				
			||||
 | 
				
			||||
    void copyData(const char *source, int sourceLength); | 
				
			||||
 | 
				
			||||
    bool equals(const char *source, int sourceLength) const; | 
				
			||||
 | 
				
			||||
    void addString(const char *source, int sourceLength); | 
				
			||||
public: | 
				
			||||
    explicit String(int size); | 
				
			||||
 | 
				
			||||
    String(const char *data, int length); | 
				
			||||
 | 
				
			||||
    explicit String(const char *source); | 
				
			||||
 | 
				
			||||
    String(const String &source); | 
				
			||||
 | 
				
			||||
    String(); | 
				
			||||
 | 
				
			||||
    ~String(); | 
				
			||||
 | 
				
			||||
    bool operator==(const String &source) const; | 
				
			||||
 | 
				
			||||
    bool operator==(const char *source) const; | 
				
			||||
 | 
				
			||||
    bool operator>(const String &compareString) const; | 
				
			||||
 | 
				
			||||
    String &operator+=(const String &source); | 
				
			||||
 | 
				
			||||
    String &operator+=(const char *source); | 
				
			||||
 | 
				
			||||
    String &operator=(const String &source); | 
				
			||||
 | 
				
			||||
    String &operator=(const char *source); | 
				
			||||
 | 
				
			||||
    String &operator+(const char *source); | 
				
			||||
 | 
				
			||||
    String &operator+(const String &source); | 
				
			||||
 | 
				
			||||
    friend std::ostream &operator<<(std::ostream &outputStream, const String &source); | 
				
			||||
 | 
				
			||||
    friend std::istream &operator>>(std::istream &outputStream, String &source); | 
				
			||||
 | 
				
			||||
    char operator[](int i); | 
				
			||||
 | 
				
			||||
    int Length() const; | 
				
			||||
 | 
				
			||||
}; | 
				
			||||
 | 
				
			||||
#endif //WORK20220401_STRING_H
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue