c++ - C++ 中一组简单字符的问题

标签 c++ set operator-overloading

我正在学习 C++,我发现这个作业可以创建一个简单的字符集数据结构。

(除了一些Java编程之外,我之前没有编程经验......)

我正在阅读的这本书给了我一些代码,我尝试复制这些代码……但它似乎不起作用。

下面的代码只输出“The Set is { }”。

谁能告诉我我做错了什么?

我感觉我在 add 函数中做错了……我不确定我是否完全理解 C++ 中的运算符重载,但是这本书写的和我一样……所以……

/*

A set class for characters

*/

#include <iostream>

using namespace std;

const int maxSize = 100;

class Set {
    char members[maxSize]; // Array that holds the set
    int len; // number of members

    // notice that find() function is private
    int find(char c); // finds an element

public:

    // Constucts a null set
    Set() {
        len = 0;
    }

    int getLength() {
        return len;
    }

    void showset();
    bool isMember(char c);

    Set operator +(char c); // add element
    Set operator -(char c); // remove element

    Set operator +(Set ob2); // set union
    Set operator -(Set ob2); // set difference
};

//returns -1 if not found.

int Set::find(char c) {
    int i;

    for (i = 0; i < len; i++) {
        if (members[i] == c) return i;

        return -1;
    }
}


//prints the set.
void Set::showset() {
    cout << "The Set is "<< "{ ";
    for (int i = 0; i < len; i++) {
        cout << members[i] << " ";
    }
    cout << " }" << "\n";
}

//checks if a character is a member of the set
bool Set::isMember(char c) {
    if (find(c) != -1) return true;
    return false;
}

Set Set::operator +(char c) {
    Set newset;

    if (len == maxSize) {
        cout << "don't add elements to a full set dumbass...";
        return *this; //existing set
    }

    //does element already exist?
    if (find(c) == -1) { //if not then add
        newset.members[newset.len] = c;
        newset.len++;
    }

    return newset;
}

//overloads - operator so it removes element
Set Set::operator -(char c) {
    Set newset;
    int i = find(c);

    //following for loop copies every element of the previous set into newset EXCEPT the one to be deleted
    for (int j = 0; j < len; j++) {
        if (j != i) {
            newset = newset + members[j];
        }
    }

    return newset;
}

// set union
Set Set::operator +(Set ob2) {
    Set newset = *this;

    //Add unique elements from second set
    for (int i = 0; i < ob2.len; i++) {
        newset = newset + ob2.members[i];
    }

    return newset;
}

//set difference
Set Set::operator -(Set ob2) {
    Set newset;

    //subtracts elements from second set
    for (int i = 0; i < ob2.len; i++) {
        newset = newset - ob2.members[i];
    }

    return newset;
}

//demonstration
int main() {
    Set s1;
    Set s2;
    Set s3;

    s1 = s1 + 'A';
    s1 = s1 + 'B';
    s1 = s1 + 'C';

    s1.showset();
    return 0;

}

最佳答案

for (i = 0; i < len; i++) {
    if (members[i] == c) return i;

    return -1;
}

return -1 应该在 for 循环之外。您的查找函数始终返回 0。(启用并注意编译器警告!)

除此之外,len 永远不会更新到 0 之后:

Set Set::operator +(char c) {
Set newset;

if (len == maxSize) {
    cout << "don't add elements to a full set dumbass...";
    return *this; //existing set
}

//does element already exist?
if (find(c) == -1) { //if not then add
    newset.members[newset.len] = c;
    newset.len++;
}

return newset;
}

在上面的 newset.len 中将始终为 0,因为 newset 每次都是一个新对象。

... 当然还有强制性的:如果你使用的是 C++ 并且需要一套你总是可以使用在 std 中找到的那个

关于c++ - C++ 中一组简单字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21555381/

相关文章:

c++ - pthreads 和快速排序的问题

C++ push_back, non const 复制构造函数

java - Set接口(interface)如何保证不重复

java - 如何将值添加到 Map 中的 Set?

c++ - 重载 operator<< 以接受模板函数

c++ - g++ 中包含哪些库?

C++ 信号到 Qt 中的 QML 槽

c++ - 将结构插入集合 C++ 时遇到问题

c++ - 重载 operator<<(ostream&, T) 其中 T 是 "enum class MyEnum"

c++ - 为什么 Crypto++ SecByteBlock 下标有效