C++ 使用 upper_bound 而不是 find()
C++ Using upper_bound instead of find ()
我需要有关如何修改程序的建议,以便我可以使用 upper/lower_bound 功能进行搜索。对于 find () 函数,该程序可以正常工作(确定断言)。
我在每次插入或删除后对公司数据库进行排序,所以这可能不是问题所在
在 link 中,我附上了整个程序以便更好地理解。
https://onecompiler.com/cpp/3xxyvx8vw
bool Company::operator == (Company cmpx) const
{
return ( ( (strcasecmp(addr.c_str(), cmpx.addr.c_str()) == 0)
&& (strcasecmp(name.c_str(), cmpx.name.c_str()) == 0) )
|| (id == cmpx.id)
);
}
bool Company::operator < (Company cmpx) const
{
return id < cmpx.id;
}
bool CVATRegister::cancelCompany ( const string &taxID )
{
Company cmp("", "", taxID);
//auto itr = upper_bound(DCompany.begin(), DCompany.end(), cmp); THIS NOT WORK
auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end())
{
DCompany.erase(itr);
sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
{
return a.getId() < b.getId();
});
return true;
}
return false;
}
您可以使用lower_bound
查找然后与taxID
进行比较
像这样:
bool CVATRegister::cancelCompany(const string &taxID)
{
Company cmp("", "", taxID);
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
//auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if (itr != DCompany.end() && itr->getId() == taxID) {
DCompany.erase(itr);
sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b) {
return a.getId() < b.getId();
});
return true;
}
return false;
}
如果 DCompany
是根据 Company::operator<
排序的(即根据公司税号),那么您可以:
bool CVATRegister::cancelCompany ( const string &taxID )
{
Company const cmp("", "", taxID);
auto const itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end() && itr->getId() == taxId)
{
DCompany.erase(itr);
// 1. No need to pass a lamda to sort your container again,
// since your Company::operator< will be called.
// 2. No need to sort again your container after removing an
// element: it will already be sorted.
//sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
//{
// return a.getId() < b.getId();
//});
return true;
}
return false;
}
我需要有关如何修改程序的建议,以便我可以使用 upper/lower_bound 功能进行搜索。对于 find () 函数,该程序可以正常工作(确定断言)。 我在每次插入或删除后对公司数据库进行排序,所以这可能不是问题所在 在 link 中,我附上了整个程序以便更好地理解。 https://onecompiler.com/cpp/3xxyvx8vw
bool Company::operator == (Company cmpx) const
{
return ( ( (strcasecmp(addr.c_str(), cmpx.addr.c_str()) == 0)
&& (strcasecmp(name.c_str(), cmpx.name.c_str()) == 0) )
|| (id == cmpx.id)
);
}
bool Company::operator < (Company cmpx) const
{
return id < cmpx.id;
}
bool CVATRegister::cancelCompany ( const string &taxID )
{
Company cmp("", "", taxID);
//auto itr = upper_bound(DCompany.begin(), DCompany.end(), cmp); THIS NOT WORK
auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end())
{
DCompany.erase(itr);
sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
{
return a.getId() < b.getId();
});
return true;
}
return false;
}
您可以使用lower_bound
查找然后与taxID
像这样:
bool CVATRegister::cancelCompany(const string &taxID)
{
Company cmp("", "", taxID);
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
//auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if (itr != DCompany.end() && itr->getId() == taxID) {
DCompany.erase(itr);
sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b) {
return a.getId() < b.getId();
});
return true;
}
return false;
}
如果 DCompany
是根据 Company::operator<
排序的(即根据公司税号),那么您可以:
bool CVATRegister::cancelCompany ( const string &taxID )
{
Company const cmp("", "", taxID);
auto const itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end() && itr->getId() == taxId)
{
DCompany.erase(itr);
// 1. No need to pass a lamda to sort your container again,
// since your Company::operator< will be called.
// 2. No need to sort again your container after removing an
// element: it will already be sorted.
//sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
//{
// return a.getId() < b.getId();
//});
return true;
}
return false;
}