要向std::unordered_set插入元素,可以使用insert()函数。

下面是一个示例代码:

#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> mySet;

    // 向unordered_set插入元素
    mySet.insert(1);
    mySet.insert(2);
    mySet.insert(3);

    // 遍历unordered_set
    for (const auto& element : mySet) {
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果:

3 2 1

在上面的示例中,我们创建了一个名为mySet的std::unordered_set对象,并使用insert()函数向其中插入了三个整数。然后,我们使用范围遍历循环遍历unordered_set,并将每个元素输出到控制台

stdunordered_set 插入元素

原文地址: https://www.cveoy.top/t/topic/iamg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录