Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;,如何让此函数删除存储在数组中的字符串?_C++ - Fatal编程技术网

C++ C++;,如何让此函数删除存储在数组中的字符串?

C++ C++;,如何让此函数删除存储在数组中的字符串?,c++,C++,我有一个delete函数,它应该通过使用前面的字符串在数组中写入来删除该字符串。 look函数see的Overide匹配,应该删除。但是我在Delete中为循环编写的代码没有删除Overide占用的数组中的第一个点,并且输出保持不变。 而且+之后的每个短语都被添加到数组中,因此数组中有四个点,很抱歉,我无法使该部分看起来更好,因为格式错误 int AR::Look(const std::string & word) { int result = -1; for(int i

我有一个delete函数,它应该通过使用前面的字符串在数组中写入来删除该字符串。 look函数see的Overide匹配,应该删除。但是我在Delete中为循环编写的代码没有删除Overide占用的数组中的第一个点,并且输出保持不变。 而且+之后的每个短语都被添加到数组中,因此数组中有四个点,很抱歉,我无法使该部分看起来更好,因为格式错误

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

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



AR their

    Ar(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;
int-AR::Look(const-std::string和word)
{
int结果=-1;

对于(int i=0;i您正在查找字符串,但仅在未出现错误时使用该值写入错误;如果您在位置N处找到该字符串,您仍将删除第一个字符串:

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++;)  <--- Why don't you use loc here???
        {
            con[i]= con[i+1];
        }
    }
}

不确定这是否是你的问题,但这不应该是这样吗

void AR::Delete(const string & word)
{
    int loc = Look(word);

    if (loc == -1)
    {
         cout<<"word not found\n";
    }
    else
    {
        for(int i=loc;i<counter-1,i++;)  // changes in this line
        {
           con[i]= con[i+1];
        }
    }
}    
void AR::Delete(常量字符串和单词)
{
int loc=外观(单词);
如果(loc==-1)
{
不能尝试以下方法:

int AR::Look(const std::string & word)
{
    for (int i = 0; i < counter; ++i)
    {
        if (con[i].find(word) != std::string::npos)
            return i;
    }
    return -1;
}

void AR::Delete(const string & word)
{
    int loc = Look(word);

    if (loc == -1)
    {
         cout << "word not found" << endl;
    }
    else
    {
         for (int i = loc+1; i < counter; ++i)
         {
             con[i-1] = con[i];
         }
         --counter;
    }
}    
int-AR::Look(const-std::string和word)
{
对于(int i=0;i<计数器;++i)
{
if(con[i].find(word)!=std::string::npos)
返回i;
}
返回-1;
}
void AR::Delete(常量字符串和单词)
{
int loc=外观(单词);
如果(loc==-1)
{

也不能添加声明(即header file.h)。具体来说,如果你包含成员变量会更有帮助。你,像这里的许多海报一样,需要学习如何调试。这里有一个不错的帖子:哦,当然,你的“删除”每次都会重复最后一个字(因为你做
con[i]=con[i+1]
但不要删除
con[i+1]
int AR::Look(const std::string & word)
{
    for (int i = 0; i < counter; ++i)
    {
        if (con[i].find(word) != std::string::npos)
            return i;
    }
    return -1;
}

void AR::Delete(const string & word)
{
    int loc = Look(word);

    if (loc == -1)
    {
         cout << "word not found" << endl;
    }
    else
    {
         for (int i = loc+1; i < counter; ++i)
         {
             con[i-1] = con[i];
         }
         --counter;
    }
}