阅读量:125
在C++中,可以使用以下方法遍历set:
- 使用迭代器:
#include
#include
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
- 使用范围-based for 循环:
#include
#include
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
for (int num : mySet) {
std::cout << num class="hljs-string">" ";
}
return 0;
}
这两种方法都可以用来遍历set中的元素。