c++ - StringSet 类中的运算符 << 出现问题
问题描述
我正在定义自己的字符串类,称为StringSet
使用字符串向量。我被分配重载>>
, <<
, ==
, >
, >=
,和运算符+
,并遇到了. 输出应该是:+=
*
<<
Welcome to stringset
hi everyone
"all" does not exist in the set.
hi
但它似乎跳过了第二行和第三行。我对重载运算符非常陌生,所以我可能忽略了一个明显的错误。
标题和类声明:
#include <iostream>
#include <vector>
#include<string>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
class StringSet
{
public:
//Constructor
StringSet();
//Copy Constructor
StringSet(const StringSet& s);
//Default constructors
StringSet(string initialStrings[], const int ARRAYSIZE);
//Destructor
~StringSet();
void add(const string s);
void remove(const string s);
//Returns length
int size()
{
return length;
}
// Overload the << operator so that it outputs the strings
friend ostream& operator <<(ostream& outs, const StringSet& s);
private:
//size of the vector
int length;
// Vector to store strings
vector <string> data;
};
函数定义:
ostream& operator<<(ostream& outs, const StringSet& s)
{
outs << "\n";
for (int i = 0; i < s.length; i++)
{
outs << s.data[i] << " ";
}
outs << "\n";
return outs;
}
//Add a string to the vector
void StringSet::add(const string s)
{
bool c = check(s);
if (c == false)
{
data.push_back(s);
}
else
{
cout << "\"" << s << "\" already exists in the set.";
}
}
// Remove a string from the vector
void StringSet::remove(const string s)
{
bool c = check(s);
if (c == true)
{
vector<string>::iterator position = search(s);
data.erase(position);
}
else
{
cout << "\"" << s << "\" does not exist in the set\n";
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
// Check if a string exists in the vector
bool StringSet::check(const string s)
{
vector<string>::iterator it = find(data.begin(), data.end(), s);
if (it != data.end())
{
return true;
}
else
{
return false;
}
}
主功能:
int main()
{
ofstream outs;
ifstream ins;
StringSet doc1, doc2, query
cout << "Welcome to stringset\n";
doc1.add("hi");
doc1.add("everyone");
outs << doc1;
doc1.remove("everyone");
doc1.remove("all");
outs << doc1;
}
解决方案
如果您使用存储集合大小的变量,则应在添加/删除元素时递增/递减它。您还可以更改的定义StringSet::size()
:
int size() const
{
return static_cast<int>(data.size());
}
推荐阅读
- interbase - 算术异常无法使用 IB.NETDataProvider 在字符集之间音译字符
- angular - (change)="onSortedSelected($event.target.value) 在 Agular 上出错,“EventTarget”类型上不存在属性值
- google-apps-script - 我可以减少 6 分钟的超时时间吗?
- html - 如何将链接对齐到右下角?
- postgresql - 在heroku上部署时npx create-react-app不起作用
- reactjs - 如何使用 React 从 DRF 获取不记名令牌?
- python - 我想用每一个用于预测的新数据来不断训练模型
- mongodb - 如何在 mongo 文本索引中进行精确的完整文本匹配,而不是精确的子字符串匹配
- reactjs - 无法显示来自 API 对象的数据
- flutter - 在隔离计算功能中运行 Parse LiveQuery 时遇到问题