C++ boost::multi_index_容器中的部分字符串搜索

C++ boost::multi_index_容器中的部分字符串搜索,c++,boost,multi-index,string-search,C++,Boost,Multi Index,String Search,我有一个用于存储个人信息的结构和用于存储此类对象的多索引容器。多索引用于按不同条件进行搜索 我已将几个人添加到容器中,并希望按姓氏查找此人。如果我使用全名,效果会很好。但是,如果我试图通过姓氏的一部分或姓氏的第一个字母来查找某人,则不会返回任何结果 正如您所知,部分字符串搜索是std::set的魅力所在。因此,我只通过一个结构来包装字符串,并失去了该功能 以下是可编译代码: #include <iostream> #include <string> #include &l

我有一个用于存储个人信息的结构和用于存储此类对象的多索引容器。多索引用于按不同条件进行搜索

我已将几个人添加到容器中,并希望按姓氏查找此人。如果我使用全名,效果会很好。但是,如果我试图通过姓氏的一部分或姓氏的第一个字母来查找某人,则不会返回任何结果

正如您所知,部分字符串搜索是std::set的魅力所在。因此,我只通过一个结构来包装字符串,并失去了该功能

以下是可编译代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <set>

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

#define DEFAULT_ADDRESS "Moscow"
#define DEFAULT_PHONE "11223344"

typedef unsigned int uint;

using namespace boost;
using namespace boost::multi_index;

struct person
{
    std::string m_first_name;
    std::string m_last_name;
    std::string m_third_name;
    std::string m_address;
    std::string m_phone;

    person();
    person(std::string f, std::string l, std::string t = "", std::string a = DEFAULT_ADDRESS, std::string p = DEFAULT_PHONE) : 
        m_first_name(f), m_last_name(l), m_third_name(t), m_address(a),
        m_phone(p) { }

    virtual ~person()
        { /*std::cout << "Destructing person..." << std::endl;*/ }

    person& operator=(const person& rhs);
};

typedef multi_index_container<
    person,
    indexed_by<
        ordered_unique<identity<person> >,
        ordered_non_unique<
            composite_key<
                person,
                member<person, std::string, &person::m_last_name>,
                member<person, std::string, &person::m_first_name>,
                member<person, std::string, &person::m_third_name>
            >
        >
    >
> persons_set;

person& person::operator=(const person &rhs)
{
    m_first_name = rhs.m_first_name;
    m_last_name = rhs.m_last_name;
    m_third_name = rhs.m_third_name;
    m_address = rhs.m_address;
    m_phone = rhs.m_phone;
    return *this;
}

bool operator<(const person &lhs, const person &rhs)
{
    if(lhs.m_last_name == rhs.m_last_name)
    {
        if(lhs.m_first_name == rhs.m_first_name)
            return (lhs.m_third_name < rhs.m_third_name);

        return (lhs.m_first_name < rhs.m_first_name);
    }
        return (lhs.m_last_name < rhs.m_last_name);
}

std::ostream& operator<<(std::ostream &s, const person &rhs)
{
    s << "Person's last name: " << rhs.m_last_name << std::endl;
    s << "Person's name: " << rhs.m_first_name << std::endl;
    if (!rhs.m_third_name.empty())
        s << "Person's third name: " << rhs.m_third_name << std::endl;
    s << "Phone: " << rhs.m_phone << std::endl;
    s << "Address: " << rhs.m_address << std::endl << std::endl;
    return s;
}

struct comp_persons
{
    bool operator()( const person& p1, const person& p2) const
    {
        if (p2.m_last_name.empty()) return false;
        return ( p1.m_last_name.find(p2.m_last_name) == 0 );
    }
};



int main()
{    
    persons_set my_set;

    persons_set::nth_index<0>::type &general_index = my_set.get<0>(); // shortcut to the 1st index
    persons_set::nth_index<1>::type &names_index = my_set.get<1>();   // shortcut to the 2nd index

    // adding persons
    general_index.insert(person("Alex", "Johnson", "Somename"));
    general_index.insert(person("Alex", "Goodspeed"));
    general_index.insert(person("Peter", "Goodspeed"));
    general_index.insert(person("Akira", "Kurosava"));

    // search via 2nd index (based on last_name)
    std::pair<persons_set::nth_index<1>::type::const_iterator, persons_set::nth_index<1>::type::const_iterator> 
        n_it = names_index.equal_range("Goodspeed");

    // this finds nothing
    /*std::pair<persons_set::nth_index<1>::type::const_iterator, persons_set::nth_index<1>::type::const_iterator> 
        n_it = names_index.equal_range("Goodspe");*/

    // idea by Kirill V. Lyadvinsky. This code crashes on the start.
    // I guess because behaviour of comp_persons differs from default less<> or reloaded operator <
    /*std::pair<persons_set::nth_index<1>::type::const_iterator, persons_set::nth_index<1>::type::const_iterator> 
        n_it = std::equal_range(names_index.begin(), names_index.end(), person("Alex", "Goodspe"), comp_persons());*/

    std::copy( n_it.first ,n_it.second,
        std::ostream_iterator<person>(std::cout));

    return 0;

}
您可以将等距或下界与自定义比较函子一起使用。可能会出现以下未测试的情况:

struct comp_substr {
  bool operator()( const char* input, const std::string& s) const {
    if ( s.empty() ) return false;
  return ( s.find( input ) == 0 );
}

// ...
// use it as follows
n_it = names_index.equal_range( "Good", comp_substr() );
您可以将等距或下界与自定义比较函子一起使用。可能会出现以下未测试的情况:

struct comp_substr {
  bool operator()( const char* input, const std::string& s) const {
    if ( s.empty() ) return false;
  return ( s.find( input ) == 0 );
}

// ...
// use it as follows
n_it = names_index.equal_range( "Good", comp_substr() );

灵感来自Kirill V Lyadvinsky

下面是正确的函子:

struct comp_substr
{
    bool operator()( const char* in, const std::string s) const 
    {
        if (s.empty()) return false;
        return (!(s.find(in) == 0));
    }
    bool operator()(const std::string s, const char* in) const 
    {
        if (s.empty()) return false;
        return (!(s.find(in) == 0));
    }
};
用法相同:

n_it = names_index.equal_range( "Good", comp_substr() );

灵感来自Kirill V Lyadvinsky

下面是正确的函子:

struct comp_substr
{
    bool operator()( const char* in, const std::string s) const 
    {
        if (s.empty()) return false;
        return (!(s.find(in) == 0));
    }
    bool operator()(const std::string s, const char* in) const 
    {
        if (s.empty()) return false;
        return (!(s.find(in) == 0));
    }
};
用法相同:

n_it = names_index.equal_range( "Good", comp_substr() );

如果您提供了最小的、可编译的代码,这些代码可以复制您的问题,并且不会分散在文本的多个段落中,那么可能会有更多的人愿意尝试解决您的问题。谢谢您的建议。现在它看起来更漂亮了,源代码也可以编译了。你说的部分字符串搜索对std::set来说是什么意思?例如,你是对的。现在我完全搞糊涂了。如果你提供了复制你的问题的最小的、可编译的代码,而不是分散在几段文字中,你可能会有更多的人愿意尝试解决你的问题。谢谢你的建议。现在它看起来更漂亮了,源代码也可以编译了。你说的部分字符串搜索对std::set来说是什么意思?例如,你是对的。现在我完全糊涂了。首先谢谢你的主意。我已将其添加到帖子中。实现有点不同,因为只有std::equal_range接受谓词,而不是容器的equal_range。您使用哪一版本的Boost?在您的案例中,您应该使用与订单相同的范围。Boost 1.42也包含此功能。首先感谢您的想法。我已将其添加到帖子中。实现有点不同,因为只有std::equal_range接受谓词,而不是容器的equal_range。您使用哪一版本的Boost?在您的案例中,您应该使用与订单相同的范围。Boost 1.42也包含此功能。这如何回答这个问题?你的代码没有解决这个问题,没有使用multi_索引,而且,出于所有的意图和目的,不是C++吗?这是如何回答这个问题的?您的代码不能解决问题,不使用Mulk索引,出于所有意图和目的,不是C++。