c++遍历set可以使用哪些方法

   2024-10-08 2950
核心提示:在C++中,可以使用以下方法遍历set:使用迭代器:#include iostream#include setint main() {std::setint mySet = {1, 2, 3, 4,

在C++中,可以使用以下方法遍历set

使用迭代器:
#include <iostream>#include <set>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 <iostream>#include <set>int main() {    std::set<int> mySet = {1, 2, 3, 4, 5};    for (int num : mySet) {        std::cout << num << " ";    }    return 0;}

这两种方法都可以用来遍历set中的元素。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号