Visual c++ 两个字符串的字符串联

Visual c++ 两个字符串的字符串联,visual-c++,Visual C++,我正在研究这个问题 编写一个String类,并重载+运算符以混合两个字符串,从而创建一个新字符串 将每个字符串中的字符拼凑在一起,直到所有字符混合。例如 •“1234567890”+“QWERTYUIOP”=“1Q2W3E4R5T6Y7U8I9O0P” 你能帮我把两个字符串的字符连接起来吗 这是我写的代码: #include "stdafx.h" #include<iostream> #include<conio.h> #include<string> usi

我正在研究这个问题

编写一个String类,并重载+运算符以混合两个字符串,从而创建一个新字符串 将每个字符串中的字符拼凑在一起,直到所有字符混合。例如 •“1234567890”+“QWERTYUIOP”=“1Q2W3E4R5T6Y7U8I9O0P”

你能帮我把两个字符串的字符连接起来吗

这是我写的代码:

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class MY_String : public string
{
  public:
    char *rep;
    MY_String(){}
    MY_String(char *tem)
    {
      rep = new char[strlen(tem)+1] ;
      strcpy(rep,tem); 
    }
    MY_String operator + (const MY_String &rhs)
    {
      char *temp;MY_String obj11("1234567890");
      temp= new char[strlen(rep) + strlen(rhs.rep)+1];//cout<<"TEMP::"<<rhs.rep<<endl;
      temp = strcat(rep,rhs.rep);
      return MY_String(temp);
    }
};
int main()
{
    MY_String obj1("1234567890");
    MY_String obj2("QWERTYUIOP");
    MY_String obj3;

    obj3 = obj1+obj2;
    cout<<obj3.rep;
    return 0;

}
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
类MY_字符串:公共字符串
{
公众:
char*rep;
我的_字符串(){}
我的字符串(字符*tem)
{
rep=新字符[strlen(tem)+1];
strcpy(rep,tem);
}
MY_字符串运算符+(常量MY_字符串和rhs)
{
字符*温度;我的字符串obj11(“1234567890”);

temp=new char[strlen(rep)+strlen(rhs.rep)+1];//cout假设您知道如何重载
操作符+
,算法可能非常简单

 string A, B, Result;
 int As = A.size(), Bs = B.size();
 int MixingLenght = min(As,Bs);
 for (int index = 0; index < MixingLength; ++index)
 {
     Result += A[index];
     Result += B[index];
 }
字符串A、B、结果;
int As=A.size(),Bs=B.size();
int MixingLenght=min(As,Bs);
对于(int index=0;index

然后,您必须决定如何处理剩余部分(如果字符串大小不同)。您可以忽略它,也可以在
结果的末尾追加

事实上,我重载了“+”运算符,而您在这里使用了“+=”运算符。那么它是否像一个相同的东西?不,这是两个不同的东西。另外,我在解决方案中追加了
字符
(使用
+=
)。你可以用
std::string::append
来代替。哦,当然,你可以使用
+
的重复,但对我来说似乎太过分了。我不会为你写作业。嘿,听着,我不是在要求你完成作业,我也不是一个小学生。我已经写了代码。我面临的唯一问题是如何完成作业重载“+”运算符。