以下是C++中常用的字符串运算符重载代码实例:

  1. 字符串加法运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    MyString operator+(const MyString& str) {
        int len1 = strlen(m_str);
        int len2 = strlen(str.m_str);
        char* tmp = new char[len1 + len2 + 1];
        strcpy(tmp, m_str);
        strcat(tmp, str.m_str);
        MyString res(tmp);
        delete[] tmp;
        return res;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2(' World!');
    MyString str3 = str1 + str2;
    cout << str3 << endl;
    return 0;
}
  1. 字符串赋值运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    MyString& operator=(const MyString& str) {
        if (this != &str) {
            delete[] m_str;
            int len = strlen(str.m_str);
            m_str = new char[len + 1];
            strcpy(m_str, str.m_str);
        }
        return *this;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2('World!');
    str2 = str1;
    cout << str2 << endl;
    return 0;
}
  1. 字符串相等运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator==(const MyString& str) const {
        return strcmp(m_str, str.m_str) == 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2('Hello');
    if (str1 == str2) {
        cout << 'str1 equals to str2' << endl;
    }
    return 0;
}
  1. 字符串不等运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator!=(const MyString& str) const {
        return strcmp(m_str, str.m_str) != 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2('World');
    if (str1 != str2) {
        cout << 'str1 is not equal to str2' << endl;
    }
    return 0;
}
  1. 字符串小于运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator<(const MyString& str) const {
        return strcmp(m_str, str.m_str) < 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2('World');
    if (str1 < str2) {
        cout << 'str1 is less than str2' << endl;
    }
    return 0;
}
  1. 字符串大于运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator>(const MyString& str) const {
        return strcmp(m_str, str.m_str) > 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('World');
    MyString str2('Hello');
    if (str1 > str2) {
        cout << 'str1 is greater than str2' << endl;
    }
    return 0;
}
  1. 字符串小于等于运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator<=(const MyString& str) const {
        return strcmp(m_str, str.m_str) <= 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('Hello');
    MyString str2('World');
    if (str1 <= str2) {
        cout << 'str1 is less than or equal to str2' << endl;
    }
    return 0;
}
  1. 字符串大于等于运算符重载:
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char* m_str;
public:
    MyString() {
        m_str = new char[1];
        m_str[0] = '\0';
    }
    MyString(const char* str) {
        int len = strlen(str);
        m_str = new char[len + 1];
        strcpy(m_str, str);
    }
    bool operator>=(const MyString& str) const {
        return strcmp(m_str, str.m_str) >= 0;
    }
    friend ostream& operator<<(ostream& os, const MyString& str) {
        os << str.m_str;
        return os;
    }
};

int main() {
    MyString str1('World');
    MyString str2('Hello');
    if (str1 >= str2) {
        cout << 'str1 is greater than or equal to str2' << endl;
    }
    return 0;
}
C++ 字符串运算符重载代码示例

原文地址: https://www.cveoy.top/t/topic/nWUo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录