容器类似数据库的检索 [英] Container for database-like searches
问题描述
我在寻找一些STL,升压或类似容器使用索引在数据库中使用的相同方式使用搜索记录这样的查询:
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';
或
select * from table1 where field1 like 'X%';
我想过使用的std ::地图,但我可以,因为我不用来搜索开始一些文本字段,而不是那些等于。除了这一点,我需要在多个领域的合作(每个记录有6个领域,例如),所以我需要一个单独的std ::地图每一个。
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.
我可以创建一个排序的向量或列表,并使用二进制搜索(在中间读元素,看它是否比'X'或多或少地打破了每个步骤2集),但我不知道是否有一些现成的容器,我可以不重新发明轮子使用?
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?
推荐答案
的 Boost.Multi指数让你与几个指标来管理和它实现的LOWER_BOUND为性病::设置/图。你需要选择对应于该领域的指数,然后执行,如果它是一张地图或一组。
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.
接着如下可用于获取一对迭代的,拳头开始与给定preFIX中的第一项的通用功能,所述第二的第一项开始的下一个preFIX,即搜索结束
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));
}
其中,
- next_ preFIX获取基于对SortedAssociateveContainer比较(当然这个功能需要更多的参数是完全通用的,请参阅<使用一个字典顺序的下一个preFIX href=\"http://stackoverflow.com/questions/2700396/how-to-get-the-next-$p$pfix-in-c\">question).
- 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).
STARTS_WITH
的结果可以在任何范围的算法中使用(见的 Boost.Range )
The result of starts_with
can be used on any range algorithm (see Boost.Range)
这篇关于容器类似数据库的检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!