试图将std :: pair插入std :: set

时间:2011-03-11 21:33:14

标签: c++ stl

我无法理解此代码中的错误:

#include <set>
#include <utility>
#include <iostream>

using namespace std;

class A
{
    public:
        A(unsigned int a) : _a(a) { }
        A() : _a(0) { }
        unsigned int a() const { return _a; }
    private:
        unsigned int _a;
};

class B
{
    public:
        B(unsigned int b) : _b(b) { }
        B() : _b(0) { }
        unsigned int b() const { return _b; }
    private:
        unsigned int _b;
};

void display(const Point& point)
{
    //cout << "A: " << point.first.a() << ", B: " << point.second.b() << endl;
}

typedef pair <A, B> Point;
typedef set <Point> List;

main()
{
    A a(5);
    B b(9);

    List list;
    List::iterator it;
    Point point;

    point = make_pair(a, b);

    it = list.begin();

    list.insert(point); // <--- error here

    //display(point);
}

错误是这样的:

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_algobase.h:66,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_tree.h:62,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/set:60,
                 from test.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = A, _T2 = B]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_function.h:230:   instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<A, B>]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_tree.h:1170:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = std::pair<A, B>, _Val = std::pair<A, B>, _KeyOfValue = std::_Identity<std::pair<A, B> >, _Compare = std::less<std::pair<A, B> >, _Alloc = std::allocator<std::pair<A, B> >]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_set.h:411:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::pair<A, B>, _Compare = std::less<std::pair<A, B> >, _Alloc = std::allocator<std::pair<A, B> >]’
test.cpp:48:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__x->std::pair<A, B>::second < __y->std::pair<A, B>::second’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__y->std::pair<A, B>::first < __x->std::pair<A, B>::first’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__x->std::pair<A, B>::first < __y->std::pair<A, B>::first’

5 个答案:

答案 0 :(得分:11)

您正在尝试将std::set 与没有排序(std::pair的元素类型一起使用,而一个集合需要其元素具有“特定的严格弱”订购标准“。


更新 :实际上std::pair 提供了operator<(感谢 @UncleBens ),这是根据其组成部分operator<定义的;所以问题在于你的AB没有提供比较运算符;你应该为operator<A写一个B

另外,由于operator<通常对点没有意义,你可以为Point创建一个比较仿函数,并将其作为{{1}的第二个模板参数传递}}

答案 1 :(得分:8)

pairset是模板,而不是类。你需要这样做:

typedef pair<A, B> Point;
typedef set<Point> List;

当您实例化模板时,模板会成为一个类,例如std::set<int> theset;类模板 set<int>创建 set

编辑:正如phooj指出的那样,你需要A和B都有一个比较运算符operator<。见Matteo Italia的答案。

答案 2 :(得分:3)

#include <set>

int main(){

    typedef pair<int, int> pairs; //creating pair as default data type 
    pairs p[5]; //array of pair objects
    for (int i =0; i<5; i++){
        p[i].first= (i+1)*10; //inserting first element of pair
        p[i].second = (i+1); //inserting first element of pair
    }
    set<pairs> s;   //set to sort pair
    set<pairs> :: iterator it; //iterator to manipulate set

    for (int i =0; i<5; i++){
        s.insert(p[i]); //inserting pair object in set
    }

    for (it = s.begin(); it!=s.end(); it++){
        pairs m = *it; // returns pair to m

    cout<<m.first<<" "<<m.second<<endl; //showing pair elements
    }
    return 0;
}

答案 3 :(得分:1)

您没有指定setpair的元素类型。

更改行

typedef pair Pointtypedef pair<A, B> Pointtypedef set Listtypedef set<Point> List应该可以解决您的问题。

一个迂腐的评论:当您阅读代码时,将set命名为List种误导。

答案 4 :(得分:1)

对于任何用户类型,它存储在类似set / map的关联容器中,类型定义必须提供'&lt; '对它进行操作。