Partial string search in boost::multi_index_container

Posted by user361699 on Stack Overflow See other posts from Stack Overflow or by user361699
Published on 2010-06-08T19:32:32Z Indexed on 2010/06/08 21:22 UTC
Read the original article Hit count: 222

Filed under:
|
|
|

I have a struct to store info about persons and multi_index_contaider to store such objects


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) {}    
};

typedef multi_index_container >,
        ordered_non_unique,
                member,
                member
            >
        >
    >
> persons_set;

operator< and operator<< implementation for person


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; return s; }

Add several persons into container: person ("Alex", "Johnson", "Somename"); person ("Alex", "Goodspeed"); person ("Petr", "Parker"); person ("Petr", "Goodspeed");

Now I want to find person by lastname (the first member of the second index in multi_index_container)


persons_set::nth_index<1>::type &names_index = my_set.get<1>();

std::pair::type::const_iterator, persons_set::nth_index<1>::type::const_iterator> n_it = names_index.equal_range("Goodspeed");

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

It works great. Both 'Goodspeed' persons are found. Now lets try to find person by a part of a last name:


std::pair::type::const_iterator, 
        persons_set::nth_index<1>::type::const_iterator> 
        n_it = names_index.equal_range("Good");

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

This returns nothing, but partial string search works as a charm in std::set. So I can't realize what's the problem. I only wraped strings by a struct. May be operator< implementation?

Thanks in advance for any help.

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost