C++ How can we insert std::set inside std::vector -



C++ How can we insert std::set inside std::vector<std::unordered_set> -

if have

std::set<int > a; std::vector<std::unordered_set<int>> b;

and went insert a within b

method 1: can do:

std::set<int > a; std::vector<std::unordered_set<int>> b (a.begin(), a.end());

method 2, cannot this?

std::set<int > a; std::vector<std::unordered_set<int>> b; b.insert(a.begin(), a.end());

what error means?

error c2664: 'std::_vector_iterator,std::equal_to<_kty>,std::allocator<_kty>>>>> std::vector,std::equal_to<_kty>,std::allocator<_kty>>,std::allocator<_ty>>::insert(std::_vector_const_iterator,std::equal_to<_kty>,std::allocator<_kty>>>>>,unsigned int,const _ty &)' : cannot convert argument 1 'std::_tree_const_iterator>>' 'std::_vector_const_iterator,std::equal_to<_kty>,std::allocator<_kty>>>>>' intellisense: no instance of overloaded function "std::vector<_ty, _alloc>::insert [with _ty=std::unordered_set, std::equal_to, std::allocator>, _alloc=std::allocator, std::equal_to, std::allocator>>]" matches argument list argument types are: (std::_tree_const_iterator>>, std::_tree_const_iterator>>) object type is: std::vector, std::equal_to, std::allocator>, std::allocator, std::equal_to, std::allocator>>>

what solution if need deal b global unordered_set?

for method 1, can below:

std::set<int> a{ 1, 2, 3, 4, 5}; std::vector<std::unordered_set<int>> b(1, std::unordered_set<int>(a.begin(), a.end()));

live demo

for method 2 can as:

std::set<int> a{ 1, 2, 3, 4, 5}; std::vector<std::unordered_set<int>> b(1); b[0].insert(a.begin(), a.end());

live demo

or alternatively as:

std::set<int> a{ 1, 2, 3, 4, 5}; std::vector<std::unordered_set<int>> b; b.push_back({a.begin(), a.end()});

live demo

or alternatively @ben voigt suggested:

std::set<int> a{ 1, 2, 3, 4, 5}; std::vector<std::unordered_set<int>> b; b.emplace_back(a.begin(), a.end());

live demo

c++

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -