溫馨提示×

c++ 中的 set 怎獲取

c++
小樊
103
2024-11-29 19:00:56
欄目: 編程語言

在 C++ 中,std::set 是一個關聯容器,它包含一組唯一的對象

  1. 使用迭代器遍歷:
#include <iostream>
#include <set>

int main() {
    std::set<int> my_set = {1, 2, 3, 4, 5};

    for (std::set<int>::iterator it = my_set.begin(); it != my_set.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 使用范圍循環(C++11 及更高版本):
#include <iostream>
#include <set>

int main() {
    std::set<int> my_set = {1, 2, 3, 4, 5};

    for (const auto &element : my_set) {
        std::cout << element << " ";
    }

    return 0;
}

在這兩個示例中,我們首先創建了一個包含整數的 std::set,然后使用迭代器或范圍循環遍歷并打印集合中的每個元素。請注意,std::set 中的元素默認是按升序排列的。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女