C++ 设置仅添加C+中的第一个元素+;

C++ 设置仅添加C+中的第一个元素+;,c++,set,C++,Set,我有以下代码: #include <iostream> #include <set> #include <string> #include <vector> using namespace std; class Tuple : public vector<string> { private: vector <string> values; //hold values public: Tuple(){};

我有以下代码:

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

class Tuple : public vector<string> {
private:
    vector <string> values; //hold values
public:
    Tuple(){};
    ~Tuple(){};

    void add(string val);
    void print();
};

void Tuple::add(string val) {
    values.push_back(val);
}
void Tuple::print() {
    for (unsigned int i = 0; i < values.size(); i++) {
        cout << values[i] << "\t";
    }
}

int main() {
set<Tuple> temp;
Tuple t1, t2, t3;
t1.add("a");
t1.add("b");
t2.add("c");
t2.add("d");
t3.add("c");
t3.add("a");
temp.insert(t1);
temp.insert(t2);
temp.insert(t3);

set<Tuple>::iterator it;
cout << temp.size() << endl;
for (it = temp.begin(); it != temp.end(); it++) {
    Tuple temp = *it;
    temp.print();
}
return 0;
}

元组或多或少是字符串的向量。我知道集合不允许重复,但我有点困惑,为什么它不会添加“cd”或“ca”,因为它们是唯一的

您继承自
vector
,并且您还有一个成员
vector
对象。现在的问题是,
add
函数会附加到成员对象。但是比较运算符是由标准库定义的,作用于基向量。它对你的成员向量一无所知。因此,
操作符将所有对象视为空(因此彼此相等)如果要将元组定义为字符串向量,可能最简单的解决方案是使用
类型定义

typedef std::vector<std::string> Tuple;
typedef std::向量元组;
这将允许您使用字符串向量的所有功能,而无需编写新代码


顺便说一句,
std::vector
确实有重载。

什么是
Tuple
,比较运算符是如何定义的`?Tuple是我声明的类,它有字符串向量的私有成员。我没有定义任何比较运算符,但我认为它与“为什么不简单地执行std::cout@DavisPearson”有关,但我认为它与“@DavisPearson”有关,如果您提供可以编译的整个示例,我将是最好的。通过将Tuple实现为向量解决了这个问题
typedef std::vector<std::string> Tuple;