【Qt】Qt的QMap与C++的std::map在value_type上有所区别

本文详细介绍了QMap和std::map的数据结构,强调了它们value_type的区别:QMap的value_type直接为value,而std::map的value_type为包含key-value对的pair。同时,通过示例代码展示了如何使用for-range循环和std::for_each遍历QMap和std::map,解释了在遍历时的差异。

通过源码或示例去说明三个知识点:

①QMap的value_type是T,也就是key-value中的value;

②std::map的value_type是pair<const _Key, _Tp>,也就是由key和value组成的pair;

③如何使用for-rangement(for的范围循环)和std::for_each循环去遍历QMap和std::map.

// QMap源码片段
template <class Key, class T>
class QMap
{
    ...
    class iterator
    {
        ...
    public:
        typedef std::bidirectional_iterator_tag iterator_category;
        typedef qptrdiff difference_type;
        typedef T value_type;
        typedef T *pointer;
        typedef T &reference;

        ...
    };
    ...
};
// STL的map源代码片段
template <class _Key, class _Tp, class _Compare, class _Alloc>
class map {
public:
  ...

  typedef _Key                  key_type;
  typedef _Tp                   data_type;
  typedef _Tp                   mapped_type;
  typedef pair<const _Key, _Tp> value_type;
  typedef _Compare              key_compare;
    
  ...
};

 

分析编译错误:/home/shz/Project/appHDQt6/parammanager.cpp:96: error: no match for ‘operator==’ (operand types are ‘const ParamMetaData’ and ‘const T’ {aka ‘const ParamMetaData’}) In file included from ../../Qt6/6.7.3/gcc_64/include/QtCore/QMap:1, from ../appHDQt6/parammanager.h:5, from ../appHDQt6/parammanager.cpp:1: ../../Qt6/6.7.3/gcc_64/include/QtCore/qmap.h: In instantiation of ‘QMapData<AMap>::valueIsEqualTo(const T&) [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::T = ParamMetaData]::<lambda(const auto:29&)> [with auto:29 = std::pair<const QString, ParamMetaData>]’: /usr/include/c++/9/bits/predefined_ops.h:283:11: required from ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_const_iterator<std::pair<const QString, ParamMetaData> >; _Predicate = QMapData<AMap>::valueIsEqualTo(const T&) [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::T = ParamMetaData]::<lambda(const auto:29&)>]’ /usr/include/c++/9/bits/stl_algo.h:104:42: required from ‘_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::pair<const QString, ParamMetaData> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<QMapData<AMap>::valueIsEqualTo(const T&) [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::T = ParamMetaData]::<lambda(const auto:29&)> >]’ /usr/include/c++/9/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_const_iterator<std::pair<const QString, ParamMetaData> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<QMapData<AMap>::valueIsEqualTo(const T&) [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::T = ParamMetaData]::<lambda(const auto:29&)> >]’ /usr/include/c++/9/bits/stl_algo.h:3969:28: required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const QString, ParamMetaData> >; _Predicate = QMapData<AMap>::valueIsEqualTo(const T&) [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::T = ParamMetaData]::<lambda(const auto:29&)>]’ ../../Qt6/6.7.3/gcc_64/include/QtCore/qmap.h:83:30: required from ‘QMapData<AMap>::Key QMapData<AMap>::key(const T&, const Key&) const [with AMap = std::map<QString, ParamMetaData, std::less<QString>, std::allocator<std::pair<const QString, ParamMetaData> > >; QMapData<AMap>::Key = QString; QMapData<AMap>::T = ParamMetaData]’ ../../Qt6/6.7.3/gcc_64/include/QtCore/qmap.h:353:40: required from ‘Key QMap<Key, T>::key(const T&, const Key&) const [with Key = QString; T = ParamMetaData]’ ../appHDQt6/parammanager.cpp:96:43: required from here ../../Qt6/6.7.3/gcc_64/include/QtCore/qmap.h:78:58: error: no match for ‘operator==’ (operand types are ‘const ParamMetaData’ and ‘const T’ {aka ‘const ParamMetaData’}) 78 | return [&value](const auto &v) { return v.second == value; }; | ~~~~~~~~~^~~~~~~~
06-08
/home/shz/Project/appHDQt6/udptask.cpp:1353: error: no match for ‘operator<’ (operand types are ‘const QHostAddress’ and ‘const QHostAddress’) In file included from /usr/include/c++/9/utility:70, from ../../Qt6/6.7.3/gcc_64/include/QtCore/qglobal.h:15, from ../../Qt6/6.7.3/gcc_64/include/QtCore/QtCore:4, from ../appHDQt6/paramdef.h:4, from ../appHDQt6/deviceparameterstorage.h:4, from ../appHDQt6/udptask.h:4, from ../appHDQt6/udptask.cpp:15: /usr/include/c++/9/bits/stl_pair.h: In instantiation of ‘constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = QHostAddress; _T2 = short unsigned int]’: /usr/include/c++/9/bits/stl_function.h:386:20: required from ‘constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<QHostAddress, short unsigned int>]’ /usr/include/c++/9/bits/stl_tree.h:2564:8: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::pair<QHostAddress, short unsigned int>; _Val = std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int> >; _Compare = std::less<std::pair<QHostAddress, short unsigned int> >; _Alloc = std::allocator<std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int> >]’ /usr/include/c++/9/bits/stl_map.h:1169:29: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = std::pair<QHostAddress, short unsigned int>; _Tp = long long unsigned int; _Compare = std::less<std::pair<QHostAddress, short unsigned int> >; _Alloc = std::allocator<std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::pair<QHostAddress, short unsigned int>, long long unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<QHostAddress, short unsigned int>]’ ../../Qt6/6.7.3/gcc_64/include/QtCore/qmap.h:370:14: required from ‘T& QMap<Key, T>::operator[](const Key&) [with Key = std::pair<QHostAddress, short unsigned int>; T = long long unsigned int]’ ../appHDQt6/udptask.cpp:1353:32: required from here /usr/include/c++/9/bits/stl_pair.h:455:24: error: no match for ‘operator<’ (operand types are ‘const QHostAddress’ and ‘const QHostAddress’) 455 | { return __x.first < __y.first | ~~~~~~~~~~^~~~~~~~~~~ /usr/include/c++/9/bits/stl_pair.h:454:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)’ 454 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) | ^~~~~~~~ /usr/include/c++/9/bits/stl_pair.h:454:5: note: template argument deduction/substitution failed: /usr/include/c++/9/bits/stl_pair.h:455:24: note: ‘const QHostAddress’ is not derived from ‘const std::pair<_T1, _T2>’ 455 | { return __x.first < __y.first | ~~~~~~~~~~^~~~~~~~~~~ 是什么问题?
07-17
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值