2010-07-01 9 views
11

एक निजी परियोजना के लिए मैं अपना खुद का libstdC++ लागू कर रहा हूं। थोड़ा सा बिट, मैं कुछ अच्छी प्रगति कर रहा हूं। आम तौर पर, मैं कुछ बुनियादी परीक्षण मामलों के लिए http://www.cplusplus.com/reference/ से उदाहरणों का उपयोग करने के लिए यह सुनिश्चित करने के लिए उपयोग करता हूं कि मेरे पास अपेक्षाकृत काम करने की स्पष्ट कार्यक्षमता है।यह अवैध है?

आज मैं विशेष रूप से इटरेटर आधारित संस्करणों उदाहरण साइट (http://www.cplusplus.com/reference/string/string/replace/) से शब्दशः प्रतिलिपि बनाई का उपयोग कर (मैं सवाल में लाइनों का कहना है के लिए एक टिप्पणी जोड़ दिया है) के साथ std::basic_string::replace के साथ एक समस्या में भाग गया,:

// replacing in a string 
#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string base="this is a test string."; 
    string str2="n example"; 
    string str3="sample phrase"; 
    string str4="useful."; 

    // function versions used in the same order as described above: 

    // Using positions:    *123456789*12345 
    string str=base;    // "this is a test string." 
    str.replace(9,5,str2);   // "this is an example string." 
    str.replace(19,6,str3,7,6);  // "this is an example phrase." 
    str.replace(8,10,"just all",6); // "this is just a phrase." 
    str.replace(8,6,"a short");  // "this is a short phrase." 
    str.replace(22,1,3,'!');  // "this is a short phrase!!!" 

    // Using iterators:     *123456789* 
    string::iterator it = str.begin(); //^
    str.replace(it,str.end()-3,str3); // "sample phrase!!!" 

    // *** this next line and most that follow are illegal right? *** 

    str.replace(it,it+6,"replace it",7); // "replace phrase!!!" 
    it+=8;        //  ^
    str.replace(it,it+6,"is cool");  // "replace is cool!!!" 
    str.replace(it+4,str.end()-4,4,'o'); // "replace is cooool!!!" 
    it+=3;        //   ^
    str.replace(it,str.end(),str4.begin(),str4.end()); 
             // "replace is useful." 
    cout << str << endl; 
    return 0; 
} 

प्रतिस्थापन के मेरे संस्करण में एक अस्थायी स्ट्रिंग के संदर्भ में लागू किया गया है जो मैं बना देता हूं तो *this के साथ स्वैप करें। यह किसी भी इटरेटर को स्पष्ट रूप से अमान्य करता है। तो क्या मैं सही हूं कि उदाहरण अमान्य है? क्योंकि यह iterators स्टोर करता है, एक प्रतिस्थापन करता है और फिर फिर से iterators का उपयोग करता है?

मानक की मेरे प्रति (आईएसओ 14882: 2003 - 21.3p5) का कहना है:

संदर्भ संकेत दिए गए, और iterators एक basic_string अनुक्रम के तत्वों की चर्चा करते हुए का पालन करते हुए अवैध हो सकता है कि basic_string वस्तु का उपयोग करता है:

- As an argument to non-member functions swap() (21.3.7.8), 
    operator>>() (21.3.7.9), and getline() (21.3.7.9). 
- As an argument to basic_string::swap(). 
- Calling data() and c_str() member functions. 
- Calling non-const member functions, except operator[](), at(), 
    begin(), rbegin(), 
    end(), and rend(). 
- Subsequent to any of the above uses except the forms of insert() and 
    erase() which return iterators, 
    the first call to non-const member functions operator[](), at(), begin(), 
    rbegin(), end(), or rend(). 

गैर स्थिरांक सदस्य कार्यों के बारे में प्रविष्टि इस कवर करने के लिए लगता है। तो जब तक कि मुझे कुछ याद नहीं आ रहा है, तो यह कोड अमान्य इटरेटर्स का उपयोग कर रहा है? बेशक यह कोड जीसीसी के libstdC++ के साथ ठीक काम करता है, लेकिन हम सभी जानते हैं कि मानक अनुपालन तक कुछ भी साबित नहीं होता है।

+13

यह वास्तव में अवैध है, और मैंने पहले से ही एफबीआई को सूचित किया है। मुझे आशा है कि आपके पास एक अच्छा वकील होगा। –

+0

उन्हें और जेल बनाने की आवश्यकता होगी, eep। –

+1

यदि आप इस बारे में सोचते हैं - आश्चर्य मत करो। cplusplus.com बग से भरा है। –

उत्तर

2

यह replace स्थान पर संचालित होने पर यह काम करेगा। मुझे नहीं लगता कि इसे इस तरह लागू किया जाना आवश्यक है। तो हाँ, मैं कहूंगा कि आपका कोड तकनीकी रूप से अवैध है।

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