Skip to main content
added 317 characters in body
Source Link
Ahmed AEK
  • 23.2k
  • 3
  • 19
  • 50

std::map is already sorted, you can take the last k elements from the end for the greatest and k elements from the start for the smallest.

for (auto it = map.rbegin(); it != map.rend(); it++)
{
    if (v.size() == k)
    {
        break;
    }
    v.push_back(*it);
}

for an unordered_map, The standard library has a function std::partial_sort_copy to pick the largest or smallest elements of any container, which can be called with an unordered_map.


As for your question.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

The standard library has a function std::partial_sort_copy to pick the largest or smallest elements of any container, which can be called with an unordered_map.


As for your question.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

std::map is already sorted, you can take the last k elements from the end for the greatest and k elements from the start for the smallest.

for (auto it = map.rbegin(); it != map.rend(); it++)
{
    if (v.size() == k)
    {
        break;
    }
    v.push_back(*it);
}

for an unordered_map, The standard library has a function std::partial_sort_copy to pick the largest or smallest elements of any container, which can be called with an unordered_map.


As for your question.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

added 258 characters in body
Source Link
Ahmed AEK
  • 23.2k
  • 3
  • 19
  • 50

The standard library has a function std::partial_sort_copy to pick the largest or smallest elements of any container, which can be called with an unordered_map.


As for your question.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

The standard library has a function std::partial_sort_copy to pick the largest or smallest elements of any container, which can be called with an unordered_map.


As for your question.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

edited body
Source Link
Ahmed AEK
  • 23.2k
  • 3
  • 19
  • 50
std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the movecopy assignment operator which std::sort needs to movecopy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the move assignment operator which std::sort needs to move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

std::vector<typename MapType::value_type> v;

map's value_type is std::pair<const Key, T>, and const elements disable the copy assignment operator which std::sort needs to copy/move the elements.

you need to use

std::vector<std::pair<typename MapType::key_type, typename MapType::mapped_type>> v;

godbolt link


as for the translation, the error you got is:

error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = const unsigned int; _T2 = Values]

which translates to, that std::pair<const unsigned int, Values> has operator= deleted.

Source Link
Ahmed AEK
  • 23.2k
  • 3
  • 19
  • 50
Loading