pair.second与unordered_map<int, string>::iterator it 指向 pair<const int, string>

C++ 中,使用 pair.second 还是 pair->second 取决于 pair 的数据类型


📌 pair.second vs pair->second

语法适用对象说明
pair.secondstd::pair<K, V>pair 是对象,使用 . 访问成员变量
pair->secondstd::pair<K, V>*(指针)pair 是指针,使用 -> 访问成员变量

✅ 1️⃣ 直接使用 pair.second(当 pair 是对象时)

#include <iostream>
#include <utility>  // std::pair

using namespace std;

int main() {
    pair<int, string> p = {1, "Alice"};
    
    // 访问 second
    cout << "ID: " << p.first << ", Name: " << p.second << endl;  

    return 0;
}

🔹 运行结果

ID: 1, Name: Alice

📌 解释

  • p 是对象,使用 p.second 访问 pair 的第二个值。

✅ 2️⃣ 使用 pair->second(当 pair 是指针时)

#include <iostream>
#include <utility>

using namespace std;

int main() {
    pair<int, string>* p = new pair<int, string>(1, "Alice");

    // **指针访问成员变量**
    cout << "ID: " << p->first << ", Name: " << p->second << endl;

    delete p;  // 释放内存
    return 0;
}

🔹 运行结果

ID: 1, Name: Alice

📌 解释

  • ppair<int, string>* 指针,所以用 p->second 访问 second 成员。

🚀 3️⃣ 在 unordered_map 遍历时的用法

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
    unordered_map<int, string> myMap = {{1, "Alice"}, {2, "Bob"}};

    // 遍历 `unordered_map`
    for (auto it = myMap.begin(); it != myMap.end(); ++it) {
        cout << "Key: " << it->first << ", Value: " << it->second << endl;
    }

    return 0;
}

🔹 运行结果

Key: 1, Value: Alice
Key: 2, Value: Bob

📌 解释

  • unordered_map<int, string>::iterator it 指向 pair<const int, string>
  • it 是指针,所以用 it->second 访问 second

🚀 总结

用法pair 类型正确写法
pair 是对象std::pair<int, string> p;p.second
pair 是指针std::pair<int, string>* p;p->second
unordered_map 迭代器中unordered_map<int, string>::iterator itit->second

🚀 规则:

  • 对象用 .pair.second
  • 指针用 ->pair->second
  • 迭代器 it->second 适用于 unordered_mapmap 🎯
/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值