Search code examples
c++compiler-errorssetmultiset

Error in Accessing Multiset element C++


I get the error

../src/internet-stack/mp-tcp-socket-impl.cc: In member function ‘void ns3::MpTcpSocketImpl::OpenCWND(uint8_t, uint32_t)’:
../src/internet-stack/mp-tcp-socket-impl.cc:2471: error: no match for ‘operator-’ in ‘sFlow->ns3::MpTcpSubFlow::measuredRTT.std::multiset<_Key, _Compare, _Alloc>::end [with _Key = double, _Compare = std::less<double>, _Alloc = std::allocator<double>]() - 1’
/usr/include/c++/4.4/bits/stl_bvector.h:179: note: candidates are: ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

because I tried:

  double maxrttval = *(sFlow->measuredRTT.end() - 1);

now, in the same code double baserttval = *(sFlow->measuredRTT.begin()); works perfectly well.

I can't understand what is wrong . I have to access the last element just like I accessed the first element. Thanks for help .


Solution

  • The iterator category of multiset is BidirectionalIterator, which does not support operator+ nor operator-, they're only supported by RandomAccessIterator. But it supports operator--, so you can:

    double maxrttval = *(sFlow->measuredRTT.end()--);
    

    And you can get the last element by reverse iterator too:

    double maxrttval = *(sFlow->measuredRTT.rbegin());