我有一个函数外观,它查看一个字符串数组并返回一个值,然后将其调用到 Delete 函数中。该值中的哪个被删除。
我这里放了很多代码,只看delete函数实现的operator+函数。并查看它们在 main 函数中的用途。
运算符 + 将字符串添加到数组中,它占用数组中的四个位置。Delete 函数应该匹配第一个字符串中的第一个单词以删除它,但它告诉我没有找到单词。
#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;
}