Linux 拨号vps windows公众号手机端

c++遍历哈希表的方法是什么

lewis 9年前 (2017-01-10) 阅读数 8 #程序编程
文章标签 c++

在C++中,可以使用迭代器来遍历哈希表。以下是一个示例代码:

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, std::string> myMap = {
        {1, "One"},
        {2, "Two"},
        {3, "Three"}
    };

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

    return 0;
}

在上面的示例中,我们使用unordered_map来创建一个哈希表,并使用迭代器it来遍历哈希表。我们可以通过it->firstit->second来访问哈希表中的键和值。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门