关于STL中的set容器键和值相同的疑惑

既然set的键和值必须相同,为什么不直接只设置键?为何还要搞一个pair?

你可以把一个值存进set里,当然也把一个pair值作为一个整体存进set里,看问题的需要。

#include <set>
#include <iostream>

using namespace std;

int main()
{
    set<pair<int, int>> s = {{1, 2}, {2, 2}, {2, 2}, {2, 3}};
    for (const auto &v : s)
        cout << '(' << v.first << ',' << v.second << ')' << ' ';
    return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
(1,2) (2,2) (2,3)

拿出具体代码说明你的问题

什么啊,set不就是一个普通容器,哪有pair???