我有一个函数外观,它通过字符串数组查找并返回一个值,然后调用Delete函数。该值中的哪一个被删除。
我在这里放了很多代码,只看一下对Delete函数实现的operator +函数。并在主函数中看到它们的用法。
运算符+将字符串添加到数组中,它占用数组中的四个点。 删除函数应该与第一个字符串中的第一个单词匹配,以删除它,但它告诉我没有找到单词。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class AR
{
public:
AR();
AR(int );
AR(const AR &);
~AR(){delete []con;}
bool Full(){return counter==cap;}
int Look(const string & );
AR & operator+(const string );
void Delete(const string &);
AR & operator=(const AR &);
friend ostream & operator<<(ostream &, AR &);
friend ifstream & operator>>(ifstream & , AR &);
void Double_size();
private:
string *con;
int counter;
int cap;
};
#include "ar.h"
AR::AR()
{
counter = 0; //initializing state of class
cap = 2;
con = new string[cap];
}
AR::AR(int no_of_cells)
{
counter = 0;
cap = no_of_cells;
con = new string[cap];
}
AR::AR(const AR & Original)
{
counter = Original.counter;
cap = Original.cap;
con = new string[cap];
for(int i=0; i<counter; i++)
{
con[i] =Original.con[i];
}
}
ostream & operator<<(ostream & out, AR & Original)
{
for(int i=0; i< Original.counter; i++)
{
out<<"con[" << i <<"] = "<< Original.con[i]<<endl;
}
return out;
}
AR & AR::operator=(const AR &rhs)
{
if(this != &rhs)
{
delete []con;
counter= rhs.counter;
cap = rhs.cap;
con= new string[cap];
for(int i=0;i<counter;i++)
{
con[i]= rhs.con[i];
}
}
return *this;
}
ifstream & operator>>(ifstream & in, AR & Original)
{
Original.counter = 0;
while(!in.eof() && Original.counter<Original.cap)
{
in>>Original.con[Original.counter];
(Original.counter)++;
}
return in;
}
AR & AR::operator+(const string word)
{
if(Full()) //conditions if array is full or empty
{
Double_size(); // capacity get's doubled
}
con[counter]=word;
counter++;
return *this;
}
void AR::Double_size()
{
cap *= 2;
string *tmp = new string[cap];
for(int i=0;i<counter;i++)
{
tmp[i]= con[i];
}
delete []con;
con = tmp;
}
int AR::Look(const string & word)
{
for(int i=0;i<counter;i++)
{
if( con [i] == word)
return i;
}
return -1;
}
void AR::Delete(const string & word)
{
int loc = Look(word);
if (loc == -1)
{
cout<<"word not found\n";
}
else
{
for(int i=0;i<counter-1,i++;)
{
con[i]= con[i+1];
}
}
}
#include <iostream>
#include <string>
#include "ar.h"
using namespace std;
int main()
{
cout<<"invoking the default constructor"<<endl;
AR myAr;
cout<<"Output after default constructor called\n";
cout<<myAr<<endl<<endl;
cout<<"invoking the explicit-value constructor "<<endl;
AR yourAr(5);
cout<<"Output after explicit-value constructor called\n";
cout<<yourAr<<endl<<endl;
cout<<"invoking the copy constructor "<<endl;
AR ourAr = myAr;
cout<<"Output after copyconstructor called\n";
cout<<ourAr<<endl<<endl;
cout<<"testing overloaded operator= with chaining as a member "<<endl;
AR X, Y, Z;
X = Y = ourAr;
cout<<"Output after operator= called\n";
cout<<X<<endl<<endl;
cout<<"testing overloaded operator<< overloaded as a friend with chaining "<<endl;
cout<<X<<Y<<Z;
cout<<endl<<endl;
cout<<"testing overloaded operator+ as a member function with chaining, Double_size "
<<" and Full."<<endl;
AR theirAr(1);
theirAr + "Overload the +" + " operator as a member function " + "with chaining to add a string " + "to an Arrary object.";
cout<<theirAr<<endl<<endl;
cout<<"testing Delete and Look. <<endl;
theirAr.Delete("XXXXXX");
theirAr.Delete("Overload");
cout<<"Output after Delete and Look called\n";
cout<<theirArray<<endl<<endl;
return 0;
}
答案 0 :(得分:0)
你正在寻找一个COMPLETE字符串等于“Overload”这里
theirAr.Delete("Overload");
但是你将字符串"Overload the +"
存储在字符串数组中,因此它们之间的比较是错误的,因为"Overload"
和"Overload the +"
是不同的。如果要在字符串中找到subString,则需要使用std::string::find
之类的方法:
int AR::Look(const std::string & word)
{
int result = -1;
for(int i=0; i<counter; ++i)
{
if( con[i].find(word) != std::string::npos)
result = i;
}
return result;
}