超载&引用;c+中的(减)运算符+;用绳子工作? 我在研究如何使用C++中的运算符重载来处理类,但不能在网上找到一个清晰的概念,所以我要发布这个问题。我想基本上重载减号“-”操作符,从一个较大的字符串中移除一个子字符串。例如,第一个字符串是“helloworld”,第二个字符串是“He”,输出应该是“lloworld”
尝试以下操作:超载&引用;c+中的(减)运算符+;用绳子工作? 我在研究如何使用C++中的运算符重载来处理类,但不能在网上找到一个清晰的概念,所以我要发布这个问题。我想基本上重载减号“-”操作符,从一个较大的字符串中移除一个子字符串。例如,第一个字符串是“helloworld”,第二个字符串是“He”,输出应该是“lloworld”,c++,string,operator-overloading,C++,String,Operator Overloading,尝试以下操作: #include <iostream> #include <string> using namespace std; //prototype string operator-(string, const string &); int main() { cout << "Hello World" - "He" << endl; } string operator-(string firstOp, const st
#include <iostream>
#include <string>
using namespace std;
//prototype
string operator-(string, const string &);
int main()
{
cout << "Hello World" - "He" << endl;
}
string operator-(string firstOp, const string &secondOp)
{
//test if the firstOp contains the secondOp
if (firstOp.find(secondOp) != string::npos)
{
//erase the secondOp of the firstOp
firstOp.erase(firstOp.find(secondOp), secondOp.length());
}
return firstOp;
}
//Output: "llo World"
#包括
#包括
使用名称空间std;
//原型
字符串运算符-(string,const string&);
int main()
{
你在网上找不到一个清晰的想法了吗?你有没有试过寻找关于这个主题的教程?因为我真诚地怀疑你找不到任何东西。还有,你尝试了什么?没有你的尝试-这看起来像是为我写代码的请求。@AlgirdasPreidžius不是那样,只是不能使用正确的术语而已o找到我要找的东西,因为我是一个C++新手,刚刚搬到课堂上。我只想学习基本语法和想法。但是,谢谢。Mat。什么是真正的问题?操作员重载还是子串去除?第一个看@慢慢44 Google C++操作程序重载教程给了你很多潜在的ANSW。ers-意思是,你在提出问题之前没有做过任何研究,而且,正如你目前的“问题”所表明的那样,它太宽泛和/或不清楚你到底在问什么。@slowjoe44你问的是两件事。如何重载-
,以及如何从字符串中删除字符。为什么不先删除字符呢?