-
Hi!
stuct mystuct{ int a; int b; int c; }; std::vector<mystruct> mylist; auto range = std::equal_range(mylist.begin(), mylist.end(), ....);
How to get stucts with b = 2 ?
@sitesv said in std::equal_range for structs:
How to get stucts with b = 2 ?
Why do you not use
std::find_if()
?:stuct mystuct{ int a; int b; int c; }; std::vector<mystruct> mylist; auto filter = [](const mystuct& s) { return s.b == 2; }; auto iter = std::find_if(mylist.begin(), mylist.end(), filter); while(iter != mylist.end()) { // do stuff with *iter // search next iter = std::find_if(std::next(iter), mylist.end(), filter); }
-
Don't see any relation to Qt here. The cppreference documentation has an example which matches exactly your problem.
-
@sitesv said in std::equal_range for structs:
How to get stucts with b = 2 ?
Why do you not use
std::find_if()
?:stuct mystuct{ int a; int b; int c; }; std::vector<mystruct> mylist; auto filter = [](const mystuct& s) { return s.b == 2; }; auto iter = std::find_if(mylist.begin(), mylist.end(), filter); while(iter != mylist.end()) { // do stuff with *iter // search next iter = std::find_if(std::next(iter), mylist.end(), filter); }
-
Don't see any relation to Qt here. The cppreference documentation has an example which matches exactly your problem.
@sitesv said in std::equal_range for structs:
Is it possible to use equal_range with vector of pointers?
Yes it is, but you will have to provide the comparison function yourself.
It's all shown in the documentation that @Christian-Ehrlicher linked to.