stdvectorstdstring转化为stdunordered_setstdstring
您可以使用std::unordered_set构造函数来将std::vectorstd::string转换为std::unordered_setstd::string。以下是一个示例代码:
#include <vector>
#include <string>
#include <unordered_set>
int main() {
std::vector<std::string> vec = {"apple", "banana", "orange", "apple", "grape"};
std::unordered_set<std::string> set(vec.begin(), vec.end());
// 打印set中的元素
for (const auto& s : set) {
std::cout << s << " ";
}
return 0;
}
在上面的示例中,我们首先创建了一个std::vectorstd::string,然后使用std::unordered_set的构造函数将vector中的元素转换为std::unordered_set。最后,我们使用循环遍历并打印std::unordered_set中的元素。
请注意,std::unordered_set不保留元素的顺序,并且每个元素只会出现一次。在示例中,重复的"apple"只会在std::unordered_set中出现一次
原文地址: https://www.cveoy.top/t/topic/idAg 著作权归作者所有。请勿转载和采集!