문제

Is there an easy way to add all the elements of a vector to an unordered_set? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it

도움이 되었습니까?

해결책

If you're constructing the unordered_set then:

std::vector<int> v;
std::unordered_set<int> s(v.begin(), v.end());

다른 팁

Forgive me if my syntax has any minor bugs, but you can try the std::copy function, its meant for this purpose.

std::vector<int> v;
std::unordered_set<int> s;
std::copy(v.begin(),v.end(),std::inserter(s,s.end()));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top