c++运算符重载总结

今天学了运算符的重载,对于c++,相比C语言而言语法又多且杂,难背又难记,最近真有点懈怠......还是要加油


#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>

using namespace std;

class String
{
public:
    //static int MAX_LEN;
    //friend class StringTool;
    //friend void print(const String &s1);
    String();
    String(char *str);
    ~String();
    String(const String& other);
    void myStrcat();
    void Display() const;
    void setMAXLEN(int maxLen);
    int getMAXLEN();
    String& operator=(const String& other);
    String& operator=(char *str);
    char& operator[](unsigned int index);
    const char& operator[](unsigned int index) const;
    String& operator+=(const String &s);    
    friend String operator+(const String& s1, const String& s2);
    friend ostream& operator<<(ostream &out, const String &s);
    friend istream& operator>>(istream &in,String& s);
    operator char *();
private:
    char *str_;
    static const int MAX_LEN = 1024;
    //String(const String& other);//绂佹鎷疯礉
};


#endif


#include <iostream>
#include <string.h>
#include "String.h"

using namespace std;

//int String::MAX_LEN = 1024;

String::String()
{
    str_ = new char('\0');
    cout <<  "default constrcutor String" << endl;
}

String::String(char *str)
{
    cout << "construct String" << endl;//str_ = str;
    int len = strlen(str) + 1;
    str_ = new char[len];
    memset(str_,0,len);
    strcpy(str_,str);
}

String::String(const String& other)
{
    int len = strlen(other.str_) + 1;
    str_ =  new char[len];
    memset(str_,0,len);
    strcpy(str_,other.str_);
}

String::~String()
{
    cout << "destroy String" << endl;  
    delete [] str_;
}

String::operator char*()
{
    return str_;
}

String& String::operator=(const String& other)
{
    if(this == &other)
    {
        return *this;
    }

    int len = strlen(other.str_) + 1;    
    delete [] str_;
    str_ = new char[len];
    memset(str_,0,len);
    strcpy(str_,other.str_);

    return *this;
}

String& String::operator=(char *str)
{
    cout << "operator char *str" << endl;
    
    int len = strlen(str) + 1;    
    delete [] str_;
    str_ = new char[len];
    memset(str_,0,len);
    strcpy(str_,str);

    return *this;
}

char& String::operator[](unsigned int index)
{
    cout << "no const []" << endl;
    return const_cast<char&>(static_cast<const String&>(*this)[index]);
}

const char& String::operator[](unsigned int index) const
{
    cout << "const []" << endl;
    
    return str_[index];
}

String operator+(const String& s1, const String& s2)
{
    /*
    int len = strlen(s1.str_) + strlen(s2.str_) + 1;
    char *newptr= new char[len];
    memset(newptr,0,len);
    strcpy(newptr,s1.str_);
    strcat(newptr,s2.str_);
    String temp(newptr);
    */
    
    String temp(s1);

    temp += s2;

    return temp;
}

String& String::operator+=(const String &s)
{
    int len = strlen(str_) + strlen(s.str_) + 1;
   
    char *newptr = new char[len];
    strcpy(newptr,str_);
    strcat(newptr,s.str_);
    delete [] str_;
    str_ = new char[len];
    strcpy(str_,newptr);
    return *this;
}

ostream& operator<<(ostream &out, const String& s)
{
    out << s.str_;
    return out;
}

istream& operator>>(istream &in, String& s)
{
    char buffer[1024];
    in >> buffer;
    int len = strlen(buffer) + 1;
    delete [] s.str_;
    s.str_ = new char[len];
    strcpy(s.str_,buffer);
    return in;
}

void String::Display() const
{
    cout << str_ << endl;
}

void String::setMAXLEN(int maxLen)
{
}

int String::getMAXLEN()
{
    return MAX_LEN;
}

#include <iostream>
#include "String.h"
#include "Stringtool.h"

using namespace std;

int main()
{
   #if 0
    String s1;

    s1 = "hello";
    
    cout << s1[1] << endl;
    
    s1[1] = 'E';
    
    s1.Display();

    String s2("world");
    cout << s2[1] << endl;
    //s[2] = 'L';

    String s3("kkk");

    //s3 = s1 + s2;
    s3+=s1;//s3 = s3 + s1;

    s3.Display();
    #endif
    
    String s("hello world");

    cout << s << endl;
    
    cin >> s;

    cout << s << endl;
    
    char *ptr = static_cast<char *>(s);
     
    cout << ptr << endl;

    return 0;
}


数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值