用于类似数据库搜索的容器

发布于 2024-08-30 00:57:06 字数 456 浏览 2 评论 0 原文

我正在寻找一些 STL、boost 或类似的容器,以使用与数据库中使用索引相同的方式来使用这样的查询来搜索记录:

select * from table1 where field1 starting with 'X';

或者

select * from table1 where field1 like 'X%';

我考虑过使用 std::map,但我不能,因为我需要搜索“以”某些文本“开头”的字段,而不是“等于”的字段。除此之外,我需要它在多个字段上工作(例如,每个“记录”有 6 个字段),因此我需要为每个字段提供一个单独的 std::map 。

我可以创建一个排序向量或列表并使用二分搜索(通过读取中间的元素并查看它是否大于或小于“X”,在每个步骤中将集合分成 2 个),但我想知道是否有一些现成的-制作容器我可以使用而无需重新发明轮子?

I'm looking for some STL, boost, or similar container to use the same way indexes are used in databases to search for record using a query like this:

select * from table1 where field1 starting with 'X';

or

select * from table1 where field1 like 'X%';

I thought about using std::map, but I cannot because I need to search for fields that "start with" some text, and not those that are "equal to". Beside that, I need it to work on multiple fields (each "record" has 6 fields, for example), so I would need a separate std::map for each one.

I could create a sorted vector or list and use binary search (breaking the set in 2 in each step by reading the element in the middle and seeing if it's more or less than 'X'), but I wonder if there is some ready-made container I could use without reinventing the wheel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-09-06 00:57:06

Boost.Multi-Index 允许您管理多个索引,并实现 std::set/map 的 lower_bound 。您需要选择与该字段对应的索引,然后就像它是地图或集合一样。

Next 后面跟随一个通用函数,可用于获取几个迭代器,第一个是从给定前缀开始的第一个项目,第二个是从下一个前缀开始的第一个项目,即搜索结束,

template <typename SortedAssociateveContainer>
std::pair<typename SortedAssociateveContainer::iterator, 
          typename SortedAssociateveContainer::iterator> 
starts_with(
  SortedAssociateveContainer const& coll, 
  typename SortedAssociateveContainer::key_type const& k)
{
  return make_pair(coll.lower_bound(k), 
                   coll.lower_bound(next_prefix(k));
}

其中

  • next_prefix 获取下一个使用基于 SortedAssociateveContainer 比较器的字典顺序的前缀(当然,此函数需要更多参数才能完全通用,请参阅 问题)。

starts_with 的结果可用于任何范围算法(请参阅 Boost.Range)

Boost.Multi-Index allows you to manage with several index and it implements the lower_bound as for std::set/map. You will need to select the index corresponding to the field and then do as if it was a map or a set.

Next follows a generic function that could be used to get a couple of iterators, the fist to the first item starting with a given prefix, the second the first item starting with the next prefix, i.e. the end of the search

template <typename SortedAssociateveContainer>
std::pair<typename SortedAssociateveContainer::iterator, 
          typename SortedAssociateveContainer::iterator> 
starts_with(
  SortedAssociateveContainer const& coll, 
  typename SortedAssociateveContainer::key_type const& k)
{
  return make_pair(coll.lower_bound(k), 
                   coll.lower_bound(next_prefix(k));
}

where

  • next_prefix gets the next prefix using lexicographic order based on the SortedAssociateveContainer comparator (of course this function needs more arguments to be completely generic, see the question).

The result of starts_with can be used on any range algorithm (see Boost.Range)

童话里做英雄 2024-09-06 00:57:06

std::map 没问题,或者 std::set 如果除了字符串之外没有数据。将您的前缀字符串传递到 lower_bound 中以获取在该点或该点之后排序的第一个字符串。然后向前迭代地图,直到到达末尾或找到不以您的前缀开头的元素。

std::map is fine, or std::set if there's no data other than the string. Pass your prefix string into lower_bound to get the first string which sorts at or after that point. Then iterate forward through the map until you hit the end or find an element which doesn't begin with your prefix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文