-4

我有一个函数外观,它查看一个字符串数组并返回一个值,然后将其调用到 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;
}
4

1 回答 1

0

你正在寻找一个完整的字符串等于“过载”在这里

theirAr.Delete("Overload");

但是您将字符串"Overload the +"存储在字符串数组中,因此它们之间的比较是错误的,因为"Overload""Overload the +"是不同的。如果要在字符串中查找子字符串,则需要通过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;

}
于 2013-09-18T17:11:37.767 回答