c++ using std::lower_bound to find element in array by 2 parameters
I have a problem using lower_bound to find element in array by 2 parameters.
I have a
struct person{
public:
person(const std::string CITY, const std::string ADDRESS,
const std::string REGION, const unsigned ID, std::string PARENT = "")
: m_city(CITY), m_addr(ADDRESS), m_region(REGION), m_id(ID), m_parent(PARENT)
{}
std::string m_city;
std::string m_addr;
std::string m_region;
unsigned m_id;
std::string m_parent;
};
from this structs there is a std::vector
How can I find exact person using lower_bound in vector which is already sorted by Region?
My approach works the worst way. Most of the time it doesn't find anything.
auto iteratorRegion = findPerson(m_sortedByRegion, candidatePerson);
std::vector::iterator findPerson(std::vector& ARRAY,
const person& candidatePerson) const {
auto iterator = std::lower_bound(ARRAY.begin(), ARRAY.end(),
candidatePerson, cityAdressComparator);
if (iterator != ARRAY.end() && iterator->m_city == candidatePerson.m_city &&
iterator->m_addr == candidatePerson.m_addr) {
return iterator;
}
auto iterator2 = std::lower_bound(ARRAY.begin(), ARRAY.end(),
candidatePerson, regionIDComparator);
if (iterator2 != ARRAY.end() && iterator2->m_region == candidatePerson.m_region &&
iterator2->m_id == candidatePerson.m_id) {
return iterator2;
}
return ARRAY.end();
}
All comparators looks like this inside:
if (!(realEstateL.m_city == realEstateR.m_city))
{
return realEstateL.m_city < realEstateR.m_city;
}
return realEstateL.m_addr < realEstateR.m_addr;