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
|
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <map>
#include <string>
//bool myFunction(std::pair<std::string, double> a, std::pair<std::string, double> b) {return a.second < b.second;}
template <typename T1, typename T2>
bool myFunction(std::pair<T1, T2> a, std::pair<T1, T2> b) {return a.second < b.second;}
int main ()
{
std::string myStrings[] = {"lol", "lola", "loli"};
double myDoubles[] = {3.0, 2.0, 1.0};
std::map<std::string, double> myMap;
for(int i=0; i<3; i++) // moche surement possible de faire mieux...
{
myMap[myStrings[i]] = myDoubles[i];
}
std::vector<std::pair<std::string, double> > mapCopy(myMap.begin(), myMap.end());
std::sort (mapCopy.begin(), mapCopy.end(), myFunction<std::string, double>);
for (std::map<std::string, double>::iterator it=myMap.begin(); it!=myMap.end(); ++it)
{
std::pair<std::string, double> a;
a = *it;
std::cout << a.first << " => " << a.second << '\n';
}
std::cout << '\n';
for (std::vector<std::pair<std::string, double>>::iterator it=mapCopy.begin(); it!=mapCopy.end(); ++it)
{
std::cout << it->first << " => " << it->second << '\n';
}
std::cout << '\n';
return 0;
} |
Partager