如何获得带有复合键的boost :: multi_index_container中的第一键的唯一计数 [英] How to get the distinct count of first key in boost::multi_index_container with composite key

查看:55
本文介绍了如何获得带有复合键的boost :: multi_index_container中的第一键的唯一计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost :: multi_index_container 允许构建具有不同排序和访问语义(例如关系数据库)的一个或多个索引的容器.我将 boost :: multi_index_container 与复合键结合使用来处理类似这样的事情:

boost::multi_index_container enables the construction of containers maintaining one or more indices with different sorting and access semantics like relational database. And I use boost::multi_index_container with composite key to handle something like this:

struct Person {
    Person(int id, string name):
        m_id(id),
        m_name(name)
    {
    }

    int m_id;
    string m_name;
};

typedef multi_index_container<
    Person,
    indexed_by<
        ordered_unique<
            member<Person, int, &Person::m_id>
        >,
        ordered_unique<
            composite_key<
                Person,
                member<Person, string, &Person::m_name>,
                member<Person, int, &Person::m_id>
            >
         >
    >
> Roster;

int main()
{
    Roster r;
    r.insert(Person(1, "Tom"));
    r.insert(Person(2, "Jack"));
    r.insert(Person(3, "Tom"));
    r.insert(Person(4, "Leo"));

    /* The distinct count of name must be 3, and how to get it? */
}

有没有办法像关系数据库那样获得 boost :: multi_index_container 中非唯一索引键的 distinct 计数?以及如何获取 Roster 复合键( Person :: m_name Person :: m_id )?THX!

Is that any way to get the distinct count of the non-unique index key in boost::multi_index_container like relational database? And how to get the distinct count of first key(Person::name) of Roster composite key(Person::m_name, Person::m_id)? THX!

还是只是迭代唯一的第一把钥匙的一种方法?这样我们就可以得到不同数量的第一把钥匙.

Or is it that a way to just iterate the distinct first key? So that we can get the distinct count of first key.

推荐答案

您可以利用以下事实:您知道复合索引首先是由 m_name 排序的.

You can exploit the fact that you know the composite index is order by m_name first.

这意味着您可以在其上运行标准的唯一",而无需其他排序步骤:

This means you can run a standard "unique" across it without an additional sort step:

    size_t unique_names = boost::size(r.get<by_name_id>() 
            | transformed([](Person const& p) -> std::string const& { return p.m_name; })
            | uniqued
        );

这可能是一个很好的时间/存储权衡.

This could be a good time/storage trade-off here.

在Coliru上直播

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>

struct Person {
    Person(int id, std::string name):
        m_id(id),
        m_name(name)
    {
    }

    int m_id;
    std::string m_name;
};

namespace bmi = boost::multi_index;

typedef boost::multi_index_container<
    Person,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::member<Person, int, &Person::m_id>
        >,
        bmi::ordered_unique<
            bmi::tag<struct by_name_id>,
            bmi::composite_key<
                Person,
                bmi::member<Person, std::string, &Person::m_name>,
                bmi::member<Person, int, &Person::m_id>
            >
        >
    >
> Roster;

#include <iostream>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>

using boost::adaptors::transformed;
using boost::adaptors::uniqued;

int main()
{
    Roster r;
    r.insert(Person(1, "Tom"));
    r.insert(Person(2, "Jack"));
    r.insert(Person(3, "Tom"));
    r.insert(Person(4, "Leo"));

    size_t unique_names = boost::size(r.get<by_name_id>() 
            | transformed([](Person const& p) -> std::string const& { return p.m_name; })
            | uniqued
        );

    std::cout << unique_names;
}

打印

3

这篇关于如何获得带有复合键的boost :: multi_index_container中的第一键的唯一计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆