小编qwe*_*rty的帖子

如何重载 == 运算符以查看具有字符串向量的两个对象是否相等?

我正在编写一个名为的类StringSet,它具有vector<string> dataint length作为其私有成员。

bool StringSet::operator == (StringSet d)
{
    for (int i = 0; i < length; i++)
    {
        if (data[i] == d.data[i])
        {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试像这样调用这个函数时,

StringSet doc1, doc2;
if (doc1 == doc2)
    {
        cout << "Both sentences are identical!\n";
    }
Run Code Online (Sandbox Code Playgroud)

我得到一个断言失败,说向量下标超出范围,我知道这意味着什么,但我不知道它在这里意味着什么。如果有人能指出我犯的一个明显错误,那就太好了,因为我是 C++ 的新手。

c++ vector operator-overloading

-2
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×1

operator-overloading ×1

vector ×1