阅读量:64
在C++中,可以使用迭代器来遍历hashset中的元素。以下是一个示例代码:
#include
#include
int main() {
std::unordered_set<int> hashset = {1, 2, 3, 4, 5};
std::cout << "Elements in hashset:" << std class="hljs-keyword">for (auto it = hashset.begin(); it != hashset.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std class="hljs-keyword">return 0;
}
在这个示例中,我们首先创建了一个包含一些整数的hashset。然后,我们使用一个迭代器从hashset的开始位置遍历到结束位置,打印每个元素的值。通过使用迭代器,我们可以方便地遍历hashset中的所有元素。