簡體   English   中英

類 StringSet 中的運算符 << 的問題

[英]Trouble with operator << in class 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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM