vendredi 17 juin 2022

Getting error while trying to work with a multiset of pairs

I have no idea what is wrong and how to correct it. Here is the code:

#include<bits/stdc++.h>
using namespace std;

struct dashboard {

int sum = 0;
double count = 0;
int sumsq = 0;

//median
multiset<int, int> smaller, larger;

//mode
map<int, int> mp;
multiset<pair<int, int>> mt;

void balance()
{
    if(smaller.size()<larger.size())
    {
        int x = (*larger.begin());
        smaller.insert(x);
        larger.erase(larger.find(x));
    }

    if(larger.size()+1<smaller.size())
    {
        int x = (*smaller.rbegin());
        smaller.insert(x);
        smaller.erase(smaller.find(x));
    }
}

void insert(int x)
{
    //mean
    sum+=x;
    count++;

    //var
    sumsq+=x*x;

    //median
    if(smaller.empty())
    {
        smaller.insert(x);
    }
    else if(x <= (*smaller.rbegin()))
    {
        smaller.insert(x);
    }
    else 
    {
        larger.insert(x);
    }

    balance();

    //mode
    //if it is already present in the multiset
    if((mt.find( make_pair(mp[x], x))) != mt.end()){
        mt.erase( mt.find( make_pair(mp[x], x)));
    }
    mp[x]++;
    mt.insert( make_pair(mp[x],x));
}

void remove(int x)
{
    //mean
    sum-=x;
    count--;

    //var
    sumsq-=x*x;

    //median
    if(larger.find(x) != larger.end())
    {
        larger.erase(larger.find(x));
    }
    else
    {
        smaller.erase(smaller.find(x));
    }

    balance();

    //mode
    if(mt.find({mp[x],x}) != mt.end())
    {
        mt.erase(mt.find(make_pair(mp[x],x)));
    }
    mp[x]--;
    if(mp[x]>0) mt.insert(make_pair(mp[x], x));
}

double mean()
{
    return sum/count;
}

double var()
{
    return (sumsq/count) - (mean()*mean());
}

double median()
{
    if(smaller.size()==larger.size())
    {
        return ((*smaller.rbegin())+ (*larger.begin()))/2.0;
    }
    else 
    {
        return (*smaller.rbegin());
    }

}

int mode()
{
    return mt.rbegin()->second;
}

};

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

dashboard d;
d.insert(1);
d.insert(2);
d.insert(4);

return 0;
}

The error that I am getting is this :

In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator]’: /usr/include/c++/9/bits/stl_multiset.h:776:29: required from ‘std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = int; _Compare = int; _Alloc = std::allocator; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator; std::multiset<_Key, _Compare, _Alloc>::key_type = int]’ main.cpp:35:30: required from here /usr/include/c++/9/bits/stl_tree.h:2564:8: error: expression cannot be used as a function 2563 | return (__j == end() | ~~~~~~~~~~~~~ 2564 | || _M_impl._M_key_compare(__k, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2565 | _S_key(__j._M_node))) ? end() : __j; | ~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/9/bits/stl_tree.h: In instantiation of ‘std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_equal_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = int]’: /usr/include/c++/9/bits/stl_tree.h:2180:4: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator]’ /usr/include/c++/9/bits/stl_multiset.h:503:40: required from ‘std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = int; _Alloc = std::allocator; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator; std::multiset<_Key, _Compare, _Alloc>::value_type = int]’ main.cpp:34:20: required from here /usr/include/c++/9/bits/stl_tree.h:2131:51: error: expression cannot be used as a function 2131 | __x = _M_impl._M_key_compare(__k, _S_key(__x)) ? /usr/include/c++/9/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::M_insert(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, _Arg&&, _NodeGen&) [with _Arg = const int&; _NodeGen = std::_Rb_tree<int, int, std::_Identity, int, std::allocator >::_Alloc_node; _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base*]’: /usr/include/c++/9/bits/stl_tree.h:2183:37: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator]’ /usr/include/c++/9/bits/stl_multiset.h:503:40: required from ‘std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = int; _Alloc = std::allocator; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator; std::multiset<_Key, _Compare, _Alloc>::value_type = int]’ main.cpp:34:20: required from here /usr/include/c++/9/bits/stl_tree.h:1812:10: error: expression cannot be used as a function 1811 | bool __insert_left = (__x != 0 || __p == _M_end() | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1812 | || _M_impl._M_key_compare(_KeyOfValue()(__v), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1813 | _S_key(__p))); | ~~~~~~~~~~~~~ /usr/include/c++/9/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_lower_bound(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, const _Key&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base]’: /usr/include/c++/9/bits/stl_tree.h:2562:16: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator]’ /usr/include/c++/9/bits/stl_multiset.h:776:29: required from ‘std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = int; _Compare = int; _Alloc = std::allocator; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator; std::multiset<_Key, _Compare, _Alloc>::key_type = int]’ main.cpp:35:30: required from here /usr/include/c++/9/bits/stl_tree.h:1934:6: error: expression cannot be used as a function 1934 | if (!_M_impl._M_key_compare(_S_key(__x), __k)) /usr/include/c++/9/bits/stl_tree.h: In instantiation of ‘static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type = const std::_Rb_tree_node]’: /usr/include/c++/9/bits/stl_tree.h:2131:44: required from ‘std::pair<std::_Rb_tree_node_base, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_equal_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = int]’ /usr/include/c++/9/bits/stl_tree.h:2180:4: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity; _Compare = int; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator]’ /usr/include/c++/9/bits/stl_multiset.h:503:40: required from ‘std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = int; _Alloc = std::allocator; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator; std::multiset<_Key, _Compare, _Alloc>::value_type = int]’ main.cpp:34:20: required from here /usr/include/c++/9/bits/stl_tree.h:777:16: error: static assertion failed: comparison object must be invocable with two arguments of key type 777 | static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},

Aucun commentaire:

Enregistrer un commentaire