Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to insert std::pair into std::set

Tags:

c++

stl

i can't understand what the error is in this code:

#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);
}

error is this:

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’
like image 314
xela Avatar asked Mar 11 '11 21:03

xela


People also ask

Can we store pair in set in C++?

Sets of pairs in C++ The first element is referenced as 'first' and the second element as 'second' and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.

How do you add items to a set in C++?

insert() function is an inbuilt function in C++ STL, which is defined in <set> header file. This function is used to insert elements in the set container. when we insert the element the size of the container is increased by the number of the elements inserted.

How do you put a STD in a pair?

The standard solution to add a new std::pair to a vector of pairs is using the std::emplace_back(T&&... args) function, which in-place construct and insert a pair at the end of a vector, using the specified arguments for its constructor. Note that this function is added in C++11.

How do you initialize a std pair in C++?

Another way to initialize a pair is by using the make_pair() function. g2 = make_pair(1, 'a'); Another valid syntax to declare pair is: g2 = {1, 'a'};


2 Answers

You are trying to use std::set with an element type that does not have ordering (std::pair), while a set needs that its elements have "a specific strict weak ordering criterion".


Update: actually std::pair does provide an operator< (thanks @UncleBens), that is defined in terms of the operator< of its components; so the problem lies in your A and B not providing a comparison operator; you should write an operator< for A and B.

In alternative, since an operator< in general doesn't really make sense for points, you can create a comparison functor for your Points and pass it as the second template argument for std::set.

like image 190
Matteo Italia Avatar answered Nov 09 '22 22:11

Matteo Italia


pair and set are templates, not classes. You need to do e.g:

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

A template becomes a class when you instantiate it, e.g. std::set<int> theset; creates the class set<int> from the class template set.

EDIT: As phooj pointed out, you need both A and B to have a comparison operator, operator<. See Matteo Italia's answer.

like image 28
Erik Avatar answered Nov 09 '22 23:11

Erik