2011-05-03 18 views
7

के सेट के साथ काम ढूंढना मैं कई स्ट्रिंग वाले structs को पकड़ने के लिए एक सेट का उपयोग कर रहा हूं। मैं सेट की खोज() कार्यक्षमता का उपयोग करने में सक्षम होना चाहता हूं। हालांकि, चूंकि सेट स्ट्रक्चर धारण कर रहा है, यह काम नहीं करता है। मैं खोजने के लिए स्ट्रक्चर में तारों में से एक को देखने के लिए() ढूंढना चाहता हूं। यह कैसे किया जा सकता है?structs

यहां वह कोड है जिसे मैंने उपयोग करने का प्रयास किया था। यह उस भाग को छोड़कर ठीक काम करता है जहां() का उपयोग किया जाता है।

test.cpp:30:7: error: no matching member function for call to 'find' 
    s.find("key"); 
    ~~^~~~ 
In file included from test.cpp:3: 
In file included from /usr/include/c++/4.2.1/set:65: 
/usr/include/c++/4.2.1/bits/stl_set.h:429:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument 
     find(const key_type& __x) 
    ^
/usr/include/c++/4.2.1/bits/stl_set.h:433:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument 
     find(const key_type& __x) const 
    ^
1 error generated. 

उत्तर

0

इस लिंक का संदर्भ लें: find_if using vectorlist

यह लिंक वेक्टर या टेम्पलेट सूची में तत्व को खोजने के लिए इस्तेमाल करेगा जब मैं यह संकलन करने की कोशिश

#include <iostream> 
#include <string> 
#include <set> 
using namespace std; 

struct test 
{ 
    string key; 
    string data; 
}; 

bool operator<(const test & l, const test & r) 
{ 
    return l.key < r.key; 
} 

bool operator==(const test & l, const test & r) 
{ 
    return l.key == r.key; 
} 

set<test> s; 

int main() 
{ 
    test newmember; 
    newmember.key = "key"; 
    newmember.data = "data"; 
    s.insert(newmember); 
    s.find("key"); 
} 

यहाँ त्रुटि संदेश है।

2

अपने structs को set में रखने में सक्षम होने के लिए आपको अपनी संरचना के लिए operator< निर्दिष्ट करना होगा। आप संबंधित स्ट्रिंग सदस्यों की तुलना करने से operator< वापसी परिणाम बना सकते हैं।

find का उपयोग करने में सक्षम होने के लिए आप को अपनी संरचना के लिए true वापस कर सकते हैं यदि संबंधित स्ट्रिंग सदस्य बराबर हैं।

नमूना:

// code from your question used here 

    int main() 

{ 
    test newmember; 
    newmember.key = "key"; 
    newmember.data = "data"; 

    test findMember; 
    findMember.key = "key"; 
    // as operator== and operator< doesn't care about data field we can left it be 
    // initialized by default constructor 

    s.insert(newmember); 
    s.find(findMember); 
} 

आप string पैरामीटर के साथ find() कॉल करने के लिए आप इस तरह उदाहरण के लिए अपने test struct के लिए string से एक अंतर्निहित निर्माता प्रदान कर सकते हैं चाहते हैं:

struct test { 
//... 
    test(const string &in_key) : key(in_key) {} 
//... 
}; 

लेकिन निहित के उपयोग रचनाकार एक अच्छी तकनीक नहीं है, क्योंकि इससे आपके कोड में कहीं और अप्रत्याशित रूपांतरण हो सकते हैं।

+0

ऐसा करने के लिए कोड क्या है? मैं <ऑपरेटर को अधिभारित कर सकता हूं लेकिन वही विधि == के लिए काम नहीं करती है। –

+0

@ z-buffer: मेरे उत्तर का अद्यतन संस्करण देखें। आपकी समस्या यह है कि आपको अपनी 'test' संरचना के उदाहरण के साथ 'find()' प्रदान करना होगा। – beduin

11

मैं आपको वैश्विक ऑपरेटर को अधिभारित करने के बजाय operator< और operator== को अपने स्ट्रक्चर में सुझाव देता हूं, मुझे यह बहुत साफ लगता है; उदाहरण:

struct test 
{ 
    string key; 
    string data; 

    bool operator<(const test& rhs) const 
    { 
    return key < rhs.key; 
    } 

    bool operator==(const test& rhs) const 
    { 
    return key == rhs.key; 
    } 
}; 
अब पर

अपने वास्तविक समस्या के लिए - आपके find() समारोह के लिए एक स्ट्रिंग से गुजर रहे हैं, लेकिन यह केवल प्रकार test की structs स्वीकार करता है। ऐसा करने के लिए, स्वत: परिवर्तन की एक निर्माता जोड़ते हैं, ताकि अंतिम struct इस प्रकार दिखाई देगा:

struct test 
{  
    string key; 
    string data; 

    test(const std::string& strKey = "", const std::string& strData = "") 
    : key(strKey), 
    data(strData) {} 

    bool operator<(const test& rhs) const 
    { 
    return key < rhs.key; 
    } 

    bool operator==(const test& rhs) const 
    { 
    return key == rhs.key; 
    } 
}; 

फिर find() के लिए एक स्ट्रिंग गुजर स्वचालित रूप से निर्माता फोन और केवल प्रासंगिक युक्त एक अस्थायी test struct बन जाएगा कुंजी। ध्यान दें कि इस विशेष मामले में, निर्माता को explicit घोषित नहीं किया जाना चाहिए।

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