访问 Multiset 元素 C++ 时出错

Error in Accessing Multiset element C++

我收到错误

../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&)

因为我尝试过:

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

现在,在相同的代码中 double baserttval = *(sFlow->measuredRTT.begin()); 工作得很好。

我不明白哪里出了问题。我必须像访问第一个元素一样访问最后一个元素。感谢您的帮助。

multiset is BidirectionalIterator, which does not support operator+ nor operator-, they're only supported by RandomAccessIterator 的迭代器类别。但是它支持operator--,所以你可以:

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

你也可以通过反向迭代器获取最后一个元素:

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

如果要访问多重集的最后一项,请使用 std::multiset::rbegin():

Return reverse iterator to reverse beginning Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning).

Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container.

rbegin points to the element preceding the one that would be pointed to by member end.

所以使用

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

你为什么不使用 std::advance?

it = sFlow->measuredRTT.end();
std::advance(it, -1);
double maxrttval = *it;