2

我正在尝试通过减少对我的数据库的往返来优化我的应用程序。作为这项工作的一部分,我一直在将一些表移动到内存中,并将它们存储为Boost.MultiIndex容器。

作为这个过程的副作用,我失去了对字符串进行通配符匹配的能力。例如,当表存储在 MySQL 中时,我可以这样做:

SELECT * FROM m_table WHERE myString LIKE "foo%"

但是,由于我现在使用带有 myString 键的 Boost.MultiIndex 容器,因此我似乎失去了这种能力。

显然,我可以使用 equal_range() 函数来查找与特定字符串完全匹配的所有条目:

std::pair< typename T::template index<by_name>::type::iterator,
           typename T::template index<by_name>::type::iterator > p
  = m_table.get<by_name>().equal_range(myString);

while (p.first != p.second )
{
  // do something with the EXACT matching entry
  ++p.first;
}

但似乎进行通配符匹配的唯一方法是遍历整个结构并将每个键与 boost::regex 和 boost::regex_match() 进行比较。

std::pair< typename T::template index<by_name>::type::iterator,
           typename T::template index<by_name>::type::iterator > p
  = std::make_pair(m_table.get<by_name>().begin(),m_table.get<by_name>().end());

while (p.first != p.second )
{
  boost::regex e(myRegex);
  if ( boost::regex_match(p.first->myString, e ) )
  {
     // Do something with the REGEX matching entry
  }
  ++p.first;
}

有没有更好的办法?

4

2 回答 2

1

在您的特定情况下,您可以执行 lower_bound("foo") 然后向前走寻找匹配项,直到您遇到不匹配的内容或到达容器的末尾。不过,我认为没有通用的方法来进行此查找。

于 2009-10-06T17:13:59.027 回答
1

好吧,首先你实际上不必使用 boost::regex,如果通配符足够简单,你可以通过滚动你自己的一元运算符来逃脱。我会注意到 Boost.Regex 是库中实际需要链接的少数部分之一(不是仅标题)。

至于走完整个结构的问题,我很抱歉,但这里没有太多可以为你做的……如果你不知道提前搜索的话。

If you know the parameters that you would be looking for in advance, then you can create a special view of the Multi-Index container suited to perform this task with a dedicated comparator/hasher (for example, one that only takes into account the first 3 characters).

If you were hoping for more, please provide more information regarding the kind of wildcards you want to use and the circumstances.

于 2009-10-06T17:15:17.513 回答