用于类似数据库搜索的容器
我正在寻找一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost.Multi-Index 允许您管理多个索引,并实现 std::set/map 的 lower_bound 。您需要选择与该字段对应的索引,然后就像它是地图或集合一样。
Next 后面跟随一个通用函数,可用于获取几个迭代器,第一个是从给定前缀开始的第一个项目,第二个是从下一个前缀开始的第一个项目,即搜索结束,
其中
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
where
The result of
starts_with
can be used on any range algorithm (see Boost.Range)std::map
没问题,或者std::set
如果除了字符串之外没有数据。将您的前缀字符串传递到lower_bound
中以获取在该点或该点之后排序的第一个字符串。然后向前迭代地图,直到到达末尾或找到不以您的前缀开头的元素。std::map
is fine, orstd::set
if there's no data other than the string. Pass your prefix string intolower_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.