unordered_set to vector
Before C++17
vector.insert(vector.end(), set.begin(), set.end());
After C++17
vector.reserve(set.size());
for (auto it = set.begin(); it != set.end(); ) {
vector.push_back(std::move(set.extract(it++).value()));
}
unordered_set to vector
Before C++17
vector.insert(vector.end(), set.begin(), set.end());
After C++17
vector.reserve(set.size());
for (auto it = set.begin(); it != set.end(); ) {
vector.push_back(std::move(set.extract(it++).value()));
}
move elements from vector to unordered_set
#include <iostream>
#include <vector>
#include <unordered_set>
int main()
{
std::vector<int> input({ 1, 2, 2, 1, 3, 1, 4 });
std::unordered_set<int> set(input.begin(), input.end());
for (const int &i: set) {
std::cout << i << " ";
}
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us