需要帮助使从数组中删除字符串的函数正常工作

Need help getting a function that removes a string from an array to work

本文关键字:常工作 函数 工作 字符串 帮助 数组 删除      更新时间:2023-10-16

>我有一个函数外观,它查看字符串数组并返回一个值,然后将其调用到Delete 函数中。从该值中删除。

我在这里放了很多代码,看看实现到 Delete 函数的运算符+函数。并查看它们在主函数中的用法。

运算符 + 将字符串添加到数组中,它在数组中占据四个位置。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 foundn";
    }
    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 calledn";
    cout<<myAr<<endl<<endl;
    cout<<"invoking the explicit-value constructor "<<endl;
    AR yourAr(5);
    cout<<"Output after explicit-value constructor calledn";
    cout<<yourAr<<endl<<endl;

    cout<<"invoking the copy constructor "<<endl;
    AR ourAr = myAr;
    cout<<"Output after copyconstructor calledn";
    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= calledn";
    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 calledn";
    cout<<theirArray<<endl<<endl;

    return 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;
}