std::set - Comparator bei Brace-Init setzen
-
Hallo zusammen,
nach mehrjähriger Abstinenz arbeite ich mich wieder etwas in C++ ein und bin auf ein Problem gestossen, bei dem ich nicht weiterkomme.
Ein std::set mit eigener Vergleichsfunktion ist in einer std::map "verbaut":
map<char, set<pair<char, int>, Comp>>
oder etwas schöner:
typedef std::pair<char, int> PermutationChar; typedef std::function<bool(PermutationChar, PermutationChar)> PermutationCharComparator; typedef std::set<PermutationChar, PermutationCharComparator> PermutationSet; typedef std::map<char, PermutationSet> PermutationMap; PermutationCharComparator comparePermutationChars = []( PermutationChar const& lhs, PermutationChar const& rhs) { if (lhs.second == rhs.second) { return lhs.first < rhs.first; } return lhs.second < rhs.second; };
Definiere ich mir jetzt ein solches set und füge anschließend Werte hinzu, funktioniert das auch wunderbar.
PermutationSet set(comparePermutationChars);
Allerdings soll die ganze Map mittels Brace-Init-List initialisiert werden, etwa so:
PermutationMap map = { {'1', {{'I', 1}}}, {'0', {{'O', 1}}}, {'V', {{'Y', 2}, {'U', 2}}}, {'Q', {{'O', 2}, {'0', 2}}}, {'I', {{'1', 1}}}, {'2', {{'Z', 1}}}, {'Z', {{'2', 1}}}, {'O', {{'0', 1}, {'Q', 3}}} };
Da in diesem Falle dem Set aber keine Vergleichsfunktion zugewiesen ist, kommt es, sobald ein Set-Eintrag mit mehr als einem Element erreicht wird, zu einem Fehler (konkret wird das Programm ohne Fehlermeldung beendet).
Daher die Frage, kann man / wie kann man hier die Vergleichsfunktion mit initialisieren?
LG, M.
-
PermutationMap map = { {'1', PermutationSet{{{'I', 1}}, comparePermutationChars}}, {'0', PermutationSet{{{'O', 1}}, comparePermutationChars}}, {'V', PermutationSet{{{'Y', 2}, {'U', 2}}, comparePermutationChars}}, {'Q', PermutationSet{{{'O', 2}, {'0', 2}}, comparePermutationChars}}, {'I', PermutationSet{{{'1', 1}}, comparePermutationChars}}, {'2', PermutationSet{{{'Z', 1}}, comparePermutationChars}}, {'Z', PermutationSet{{{'2', 1}}, comparePermutationChars}}, {'O', PermutationSet{{{'0', 1}, {'Q', 3}}, comparePermutationChars}} };
ungetestet.
-
Vielen Dank für deine Antwort, super, es funktioniert
Hab noch etwas getestet, mit () geht es auch:
PermutationMap map = { {'1', PermutationSet({{'I', 1}}, comparePermutationChars)}, {'0', PermutationSet({{'O', 1}}, comparePermutationChars)}, // ...undsoweiter
------
LG, M.