I'm trying to create a sorted vector
from a map
, sorted according to a value that isn't the map's key.
The map value is block
object, and I want the vector to be sorted according to size
, attribute of block
.
My code:
#include <map>
#include <string>
#include <vector>
struct block {
string data;
int size;
};
struct vecotrCompare {
bool operator()(pair<const string, block*> &left,
pair<const string, block*> &right) {
return left.second -> size < right.second -> size;
}
};
int main() {
map<const string, block*> myMap;
vector<pair<const string, block*> > myVector(
myMap.begin(), myMap.end());
sort(myVector.begin(), myVector.end(), vecotrCompare());
}
The sort(...)
line can't compile, and I'm getting a compile-error:
error: no match for call to ‘(vecotrCompare) (std::pair<const
std::basic_string<char>, block*>&, const std::pair<const
std::basic_string<char>, block*>&)’
required from here
is only part of the error, and the most useless part at that. – chrisconst
something) in a vector. It's redundant in the map, and won't work outside of that. – chrisconst&
fromvecotrCompare::operator()
's arguments. Add theconst&
back in, and you'll run into the next error, which I've explained in my answer. – Praetorian