I tried to use std::tuple as an element of std::set where std tuple consists of std::string and a bool (for this example only, in normal use case the tuple consists of 3 elements). The string of the tuple shall be unique, therefore I created comparator that would sort the elements on the first element of the tuple. However, when inserting into aforementioned set I do get duplicate elements in the container. I cannot see where I am making a mistake. Thanks in advance for suggestions!
#include <set>
#include <functional>
#include <iostream>
using namespace std;
int main() {
using tuple_t = std::tuple<std::string, bool>;
std::function<bool(const tuple_t &, const tuple_t &)> tupleCmp =
[](const tuple_t & l, const tuple_t & r) -> bool {
return static_cast<bool>(std::get<0>(l).compare(std::get<0>(r)));
};
set<tuple_t, decltype(tupleCmp)> s(tupleCmp);
s.insert(make_tuple("ahoy", false));
s.insert(make_tuple("this", false));
s.insert(make_tuple("creates", false));
s.insert(make_tuple("duplicates", false));
s.insert(make_tuple("ahoy", false));
for (const auto & i : s){
cout << get<0>(i) << endl;
}
auto debug_stop = 0;
}
Output:
ahoy
duplicates
creates
this
ahoy
Aucun commentaire:
Enregistrer un commentaire