0
votes

If we have

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

And we went to insert a inside b

Method 1: we can do:

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

Method 2, we cannot do this?

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

What does this 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 from 'std::_Tree_const_iterator>>' to '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 the 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 is the solution if we need to deal with b as global unordered_set?

2
I'm not really clear on what you're trying to do. What are you expecting the contents of b to be? - Joseph Mansfield
The error means that a set is not an unordered_set... - The Paramagnetic Croissant
What from the error message is unclear in particular for you? - πάντα ῥεῖ
You can't deal with b as global unordered_set, because it isn't a set. It is a whole vector of sets. Did you mean to add the contents of a a set already in the vector, e.g. b[0].insert(...)? Or add a new set into the vector, where the contents of the new set come from a ? - Ben Voigt
@Sandy: Try b.emplace_back(a.begin(), a.end()); - Ben Voigt

2 Answers

4
votes

For method 1, you can to it as 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 two you can do 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 as @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

2
votes

For method 2:

No overload of vector::insert() takes two iterators. Try this instead:

b.insert( b.begin(), { a.begin(), a.end() } )