1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <vector>
#include <string>
#include <utility>
#include <iostream>
#include <algorithm>
class Assoc{
std::vector<std::pair<std::string, int>> vec;
public:
int& operator[](const std::string& str);
void print() const;
};
int& Assoc::operator [](const std::string& str)
{
for(auto& x : vec )
{
if(x.first == str)
return x.second;
}
vec.push_back(std::pair<std::string, int>{str, int{}});
return vec.back().second;
}
class Compare{
public:
bool operator()(const std::pair<std::string, int>& p1, const std::pair<std::string, int>& p2){
return p1.second < p2.second;
}
};
void Assoc::print() const
{
std::sort(vec.begin(), vec.end(), Compare{}); //if I add this this gives huge error message
//print sorted vec
}
int main()
{
std::vector<std::pair<std::string, int>> vec; //Just checking if this will give the same error
vec.push_back({"Hello", 1});
vec.push_back({"World!", 2});
std::sort(vec.begin(), vec.end(), Compare{}); //This works!!!!
return 0;
}
|