C++ 在向量中使用 lower_bound 搜索并忽略小写/大写字母
C++ Search using lower_bound in the vector and ignore lowercase / uppercase letters
如果不区分大小写,如何在向量中查找元素。例如,如果我在 cmp name = "tOM" addr = "LONDON" 中,我希望它找到一个我保存在 vector 中的值为 name = "Tom" addr = "London" 的元素?我附上整个程序 https://onecompiler.com/cpp/3xy2j7dmd .
bool Company::cmpNA2 (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()) );
return ( strcasecmp(a.getAddr().c_str(), b.getAddr().c_str()));
}
bool CVATRegister::invoice ( const string &name, const string &addr, unsigned int amount )
{
Company cmp(name, addr,"-1");
sort(DCompany.begin(), DCompany.end(), [](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()) );
return ( strcasecmp(a.getAddr().c_str(), b.getAddr().c_str()));
});
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp, &Company::cmpNA2);
// cout << itr->getTotalIncome() << itr->getId() << endl; <--- Nothing
首先是 std::sort
要求您 return true
或 false
。问题是 strcasecmp
return 是一个 int
,表示第一项是在第二项 (-1, 0, 1) 之前、等于还是之后。这是三个值,不仅仅是 true
或 false
.
为了简化您的代码,您可以做类似这样的事情:
bool CVATRegister::invoice ( const string &name, const string &addr, unsigned int amount )
{
std::sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
{
return strcasecmp(a.getName().c_str(), b.getName().c_str()) < 0;
});
}
因为 strcasecmp
returns -1 如果第一项放在第二项之前,那么谓词简单地 returns true
如果结果是 < 0
,否则 false
。
如果不区分大小写,如何在向量中查找元素。例如,如果我在 cmp name = "tOM" addr = "LONDON" 中,我希望它找到一个我保存在 vector 中的值为 name = "Tom" addr = "London" 的元素?我附上整个程序 https://onecompiler.com/cpp/3xy2j7dmd .
bool Company::cmpNA2 (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()) );
return ( strcasecmp(a.getAddr().c_str(), b.getAddr().c_str()));
}
bool CVATRegister::invoice ( const string &name, const string &addr, unsigned int amount )
{
Company cmp(name, addr,"-1");
sort(DCompany.begin(), DCompany.end(), [](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()) );
return ( strcasecmp(a.getAddr().c_str(), b.getAddr().c_str()));
});
auto itr = lower_bound(DCompany.begin(), DCompany.end(), cmp, &Company::cmpNA2);
// cout << itr->getTotalIncome() << itr->getId() << endl; <--- Nothing
首先是 std::sort
要求您 return true
或 false
。问题是 strcasecmp
return 是一个 int
,表示第一项是在第二项 (-1, 0, 1) 之前、等于还是之后。这是三个值,不仅仅是 true
或 false
.
为了简化您的代码,您可以做类似这样的事情:
bool CVATRegister::invoice ( const string &name, const string &addr, unsigned int amount )
{
std::sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b)
{
return strcasecmp(a.getName().c_str(), b.getName().c_str()) < 0;
});
}
因为 strcasecmp
returns -1 如果第一项放在第二项之前,那么谓词简单地 returns true
如果结果是 < 0
,否则 false
。