-
wrote on 28 Jul 2021, 13:50 last edited by
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 ?
-
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 ?
wrote on 28 Jul 2021, 14:12 last edited by@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); }
wrote on 28 Jul 2021, 17:59 last edited by sitesvIs it possible to use equal_range with vector of pointers?
I need to edit vector elements in runtime... Example from doc is not working in this case cause of 'const'.... -
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.
5/5