2011-11-03 14 views
6

मैं 2 पूर्णांक कुंजी K1 और K2 के अनुसार वस्तुओं को संग्रहीत करने के लिए बूस्ट मल्टी_इंडेक्स कंटेनर का उपयोग कर रहा हूं। मैं उदाहरण के लिए, "के 1 == एक्स" को संतुष्ट करने वाले सभी तत्वों पर आसानी से एक पुनरावृत्ति प्राप्त कर सकता हूं, उदाहरण के लिए, पहली अनुक्रमणिका ले कर और ढूंढ() फ़ंक्शन (के 2 के लिए आईडीम और एक मान वाई) का उपयोग करके, लेकिन मैं एक रास्ता तलाश रहा हूं के 1 == एक्स और के 2 == वाई दोनों को संतुष्ट करने वाले सभी तत्वों पर एक इटेटरेटर प्राप्त करने के लिए एक स्पष्ट समाधान है कि K1 == एक्स को संतुष्ट करने वाले सभी तत्वों पर एक इटरेटर प्राप्त करना है, फिर भविष्यवाणी K2 == वाई के साथ एक boost :: filter_iterator बनाएं, लेकिन क्या बूस्ट.मुल्टीइंडेक्स से ऐसा करने का कोई तरीका है (शायद अधिक कुशलतापूर्वक)?Boost.MultiIndex: एकाधिक फ़ील्ड का उपयोग कर तत्व खोज रहे हैं

धन्यवाद

माथीउ

+0

'X' और' Y' के आधार पर एक क्रमबद्ध साहचर्य सूचकांक बनाएँ? ऐसा लगता है कि आप पहले से ही क्या कर रहे हैं। आप अपने कंटेनर घोषणा पोस्ट करना चाह सकते हैं। –

उत्तर

6

आप K1 और K2 दोनों के साथ एक boost::multi_index::composite_key उपयोग कर सकते हैं।

यहाँ

एक छोटा सा उदाहरण है, जो ideone.com पर भी है:

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

#include <iostream> 

struct Stuff 
{ 
    Stuff (int iFirst, int iSecond) 
     : m_iFirst(iFirst), 
      m_iSecond(iSecond) 
    { 
    } 

    int m_iFirst; 
    int m_iSecond; 

}; 

std::ostream& operator<<(std::ostream& rOut, Stuff const& rStuff) 
{ 
    return rOut << rStuff.m_iFirst << "/" << rStuff.m_iSecond << "\n"; 
} 


struct FirstIdx{}; 
struct SecondIdx{}; 
struct BothIdx{}; 

typedef boost::multi_index_container< 
    Stuff, 
    boost::multi_index::indexed_by< 
     boost::multi_index::ordered_non_unique<boost::multi_index::tag<FirstIdx>, boost::multi_index::member<Stuff, int, &Stuff::m_iFirst> >, 
     boost::multi_index::ordered_non_unique<boost::multi_index::tag<SecondIdx>, boost::multi_index::member<Stuff, int, &Stuff::m_iSecond> >, 
     boost::multi_index::ordered_non_unique<boost::multi_index::tag<BothIdx>, boost::multi_index::composite_key<Stuff, boost::multi_index::member<Stuff, int, &Stuff::m_iFirst>, 
                                  boost::multi_index::member<Stuff, int, &Stuff::m_iSecond> > > 
     > 
    > TDicStuffs; 

typedef TDicStuffs::index<FirstIdx>::type TFirstIdx; 
typedef TDicStuffs::index<SecondIdx>::type TSecondIdx; 
typedef TDicStuffs::index<BothIdx>::type TBothIdx; 

int main(int argc, char *argv[]) 
{ 

    TDicStuffs stuffs; 

    // fill some stuffs 
    stuffs.insert(Stuff(1, 1)); 
    stuffs.insert(Stuff(1, 2)); 
    stuffs.insert(Stuff(1, 3)); 
    stuffs.insert(Stuff(2, 1)); 
    stuffs.insert(Stuff(2, 2)); 
    stuffs.insert(Stuff(2, 3)); 
    stuffs.insert(Stuff(3, 1)); 
    stuffs.insert(Stuff(3, 2)); 
    stuffs.insert(Stuff(3, 3)); 

    assert(stuffs.size() == 9); 

    // search for m_iFirst == 2 
    TFirstIdx::const_iterator itFirstLower; 
    TFirstIdx::const_iterator itFirstUpper; 

    boost::tie(itFirstLower, itFirstUpper) = stuffs.get<FirstIdx>().equal_range(2); 

    assert(std::distance(itFirstLower, itFirstUpper) == 3); 

    std::copy(itFirstLower, itFirstUpper, std::ostream_iterator<Stuff>(std::cout << "\n")); 

    // search for m_iSecond == 3 
    TSecondIdx::const_iterator itSecondLower; 
    TSecondIdx::const_iterator itSecondUpper; 

    boost::tie(itSecondLower, itSecondUpper) = stuffs.get<SecondIdx>().equal_range(3); 

    assert(std::distance(itSecondLower, itSecondUpper) == 3); 

    std::copy(itSecondLower, itSecondUpper, std::ostream_iterator<Stuff>(std::cout << "\n")); 

    // search for m_iFirst == 2 m_iSecond == 3 
    TBothIdx::const_iterator itBothLower; 
    TBothIdx::const_iterator itBothUpper; 

    boost::tie(itBothLower, itBothUpper) = stuffs.get<BothIdx>().equal_range(boost::make_tuple(2,3)); 

    assert(std::distance(itBothLower, itBothUpper) == 1); 

    std::copy(itBothLower, itBothUpper, std::ostream_iterator<Stuff>(std::cout << "\n")); 

    return 0; 
} 
+1

असल में, तीन सूचकांक रखने की कोई आवश्यकता नहीं है: आप केवल पहले कुंजी को एक समग्र कुंजी निकालने के साथ सुसज्जित कर सकते हैं और इसे m_iFirst और on (m_iFirst, m_iSecond) पर आधारित लुकअप के लिए उपयोग कर सकते हैं। –

+2

@ जोक्विन एम लोपेज़ मुनोज: आप सही हैं, लेकिन मैंने उन्हें केवल प्रदर्शन के लिए जोड़ा। – Lars

+0

बहुत बहुत धन्यवाद, मुझे समग्र कुंजी के साथ समाधान पसंद है। – sunmat

संबंधित मुद्दे