首页 > 解决方案 > 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;
}

标签: c++classvectorsetoperator-overloading

解决方案


如果您使用存储集合大小的变量,则应在添加/删除元素时递增/递减它。您还可以更改的定义StringSet::size()

int size() const
{
    return static_cast<int>(data.size());
}

推荐阅读