要向std::unordered_setstd::string中插入元素,可以使用insert()函数。以下是一个示例代码:

#include <iostream>
#include <unordered_set>
#include <string>

int main() {
  std::unordered_set<std::string> set;

  // 插入单个元素
  set.insert("apple");

  // 插入多个元素
  set.insert({"banana", "orange", "grape"});

  // 使用迭代器插入元素
  std::unordered_set<std::string>::iterator it = set.insert("kiwi").first;

  // 输出集合中的元素
  for (const std::string& fruit : set) {
    std::cout << fruit << " ";
  }
  std::cout << std::endl;

  return 0;
}

在上述示例中,首先创建了一个std::unordered_setstd::string对象set。然后通过insert()函数向集合中插入元素。可以一次插入单个元素,也可以一次插入多个元素。还可以使用insert()函数返回的迭代器来插入元素。

最后通过遍历集合中的元素,输出每个元素的值。

输出结果为:

banana apple kiwi grape orange
``
stdunordered_setstdstring插入

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

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