'Improving the time complexity of the program - vector, find, lower_bound
I have a few questions about how to adjust my program so that the speed is O (log (n)) - at least for the invoice and audit methods.
In the newCompany method, I insert an element into a vector and then sort by ID? There is a need to sort after inserting, can't it be done differently? At the same time, I search here using the find () method, I can't use lower_bound () because I have elements sorted by ID.
I have two invoice methods. In the first I look for id, and I use lower_bound, I don't see the problem in that. I'm looking for a different variant using the addr and name string, so I can't use lower_bound again from the same problem as with the new method.
Are there any ways to replace the problems described above? Thank you in advance.
The whole program https://onecompiler.com/cpp/3xybks89x
class Company
{
...
private:
string name;
string addr;
string id;
unsigned int totalIncome;
unsigned int numberOrders;
unsigned int amount;
};
bool CVATRegister::newCompany ( const string &name, const string &addr, const string &taxID )
{
Company cmp(name, addr, taxID);
if ( find(DCompany.begin(), DCompany.end(), cmp) == DCompany.end() )
{
DCompany.push_back(cmp);
sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
{
return a.getId() < b.getId();
});
return true;
}
return false;
}
bool CVATRegister::invoice ( const string &taxID, unsigned int amount )
{
Company cmp("", "", taxID);
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end() && itr->getId() == taxID)
{
TotalInvoice.push_back(amount);
DCompany[distance(DCompany.begin(), itr)].saveInvoice(amount);
return true;
}
return false;
}
bool CVATRegister::invoice ( const string &name, const string &addr, unsigned int amount )
{
Company cmp(name, addr,"-1");
auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end())
{
TotalInvoice.push_back(amount);
DCompany[distance(DCompany.begin(), itr)].saveInvoice(amount);
return true;
}
return false;
}
bool CVATRegister::audit ( const string &name, const string &addr, unsigned int &sumIncome ) const
{
Company cmp(name, addr,"-1");
auto itr = find(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end())
{
sumIncome = itr->getTotalIncome();
return true;
}
return false;
}
bool CVATRegister::audit ( const string &taxID, unsigned int &sumIncome) const
{
Company cmp("", "", taxID);
auto const itr = lower_bound(DCompany.begin(), DCompany.end(), cmp);
if(itr != DCompany.end() && itr->getId() == taxID)
{
sumIncome = DCompany[distance(DCompany.begin(), itr)].getTotalIncome();
return true;
}
return false;
}
EDIT:
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp, [](const Company & a, const Company & b)
{
if (strcasecmp(a.getName().c_str(), b.getName().c_str()) == 0)
return strcasecmp(a.getName().c_str(), b.getName().c_str()) < 0;
return strcasecmp(a.getAddr().c_str(), b.getAddr().c_str()) < 0;
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
