在C++中,可以使用迭代器来遍历哈希表
在C++中,可以使用迭代器来遍历哈希表。以下是一个示例代码:
#include<iostream>
#include<unordered_map>
intmain(){
std::unordered_map<int,std::string>myMap={
{1,"One"},
{2,"Two"},
{3,"Three"}
};
//遍历哈希表
for(autoit=myMap.begin();it!=myMap.end();++it){
std::cout<<"Key:"<<it->first<<",Value:"<<it->second<<std::endl;
}
return0;
}
在上面的示例中,我们使用unordered_map
来创建一个哈希表,并使用迭代器it
来遍历哈希表。我们可以通过it->first
和it->second
来访问哈希表中的键和值。
版权声明
本文仅代表作者观点,不代表博信信息网立场。