【问题标题】:Trouble with operator << in class StringSetStringSet 类中的运算符 << 出现问题
【发布时间】:2020-03-07 20:44:38
【问题描述】:

我正在使用字符串向量定义我自己的字符串类StringSet。我被分配重载&gt;&gt;&lt;&lt;==&gt;&gt;=++=* 运算符,并遇到了&lt;&lt; 的问题。输出应该是:

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

【问题讨论】:

  • 此问题中显示的代码无法满足 stackoverflow.com 对minimal reproducible example 的要求,因此 stackoverflow.com 上的任何人都不太可能确定问题所在。这个问题必须是edited 以显示一个最小示例,不超过一两页代码(“最小”部分),任何人都可以剪切/粘贴、编译、运行和重现所描述的问题(“可重现”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
  • 我们至少需要看看addremove 做了什么。
  • @john 我已经添加了addremove。谢谢。
  • 现在我们需要查看check 和您的构造函数。请阅读minimal reproducible example。其实读过。您将花费更少的时间阅读、编辑您的问题并在此处获得答案,而不是花费您不断回应我们反复尝试澄清的时间 - 同时积累反对票。
  • 仍在寻找答案。如果有人在我的代码中发现关于运算符

标签: c++ class vector set operator-overloading


【解决方案1】:

如果你使用一个变量来存储集合的大小,你应该在添加/删除元素时增加/减少它。也可以更改StringSet::size()的定义:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多