Azzam
bool compareByLength(pair<string, int> v1, pair<string, int> v2){
return v1.second < v2.second;
}
sort(dict.begin(), dict.end(), compareByLength);
map<string, int> dict;
this code is throwing errors..
can you please help out as I'm not getting where the error is
Azzam
In file included from /usr/include/c++/7/algorithm:62:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
from test.cpp:1:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, int>)>]’:
/usr/include/c++/7/bits/stl_algo.h:4868:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >; _Compare = bool (*)(std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, int>)]’
test.cpp:66:55: required from here
/usr/include/c++/7/bits/stl_algo.h:1969:22: error: no match for ‘operator-’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >’ and ‘std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >’)
std::__lg(__last - __first) * 2,
~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
from /usr/include/c++/7/bits/char_traits.h:39,
from /usr/include/c++/7/ios:40,
from /usr/include/c++/7/istream:38,
from /usr/include/c++/7/sstream:38,
from /usr/include/c++/7/complex:45,
from /usr/include/c++/7/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
from test.cpp:1:
/usr/include/c++/7/bits/stl_iterator.h:392:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator-(const reverse_iterator<_IteratorL>& __x,
Chernykh
Referred to cppreference: u should pass ur comparator as a class with overloaded call operator, there are some ways to make it:
std::function - special class which made all dirty jobs instead of u, just see in docs how to use it
struct with overloaded () - see example on cppreference
lambda - should I really explain how to use it?