需要帮助使从数组中删除字符串的函数正常工作
Need help getting a function that removes a string from an array to work
>我有一个函数外观,它查看字符串数组并返回一个值,然后将其调用到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;
}
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中的memset函数工作不正常
- 将返回值存储在函数指针数组的指针中是如何工作的?
- insertElement() 函数无法按预期工作
- 即使没有调用这个递归函数,它是如何工作的?
- type_info成员函数如何工作?
- 如何定义在用作函数参数时工作的类模板的转换
- 优先级队列构造函数的工作
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?
- 使用移动和复制语义时函数匹配如何工作?
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++代码停止工作错误使用cout内部函数
- 好友函数的工作原理
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 函数调用在常量表达式中必须具有常量值
- 以下代码如何工作以每次为唯一调用堆栈唯一实例化模板函数?
- 递归函数调用在后台工作
- 如何使函数在派生类中工作?
- 工作函数重载如何使用可变参数解析?
- C++继承和具有虚拟工作函数的Pthreads