मैं ऐसे दो typedefs आ रही हैं:एसटीएल iterators std :: दूरी() त्रुटि
typedef std::vector<int> Container;
typedef std::vector<int>::const_iterator Iter;
समस्या यह है कि मैं विचार में, मैं Container Input
पर कुछ कार्रवाई करने, और उसके बाद मैं गणना करने के लिए std::distance(Input.begin(),itTarget)
चाहते हैं , जहां itTarget
Iter
प्रकार का है। लेकिन मुझे यह कंपाइलर त्रुटि मिल रही है कि no instance of function template "std::distance" matches the argument list
, और केवल कास्टिंग के बाद, यानी, std::distance(static_cast<Iter>(Input.begin()),itTarget)
सब कुछ ठीक काम करता है।
मुझे आश्चर्य है कि वह क्यों है?
क्या ऐसा इसलिए है क्योंकि 'प्रारंभ() '' const_iterator' वापस नहीं कर रहा है? –
लेकिन किसी भी तरह से यह सक्षम होना चाहिए? http://www.cplusplus.com/reference/vector/vector/begin/ –
वैसे ही 'const' और non-'const' अधिभार काम करता है। केवल तभी 'इनपुट' 'const'-योग्य है 'const' अधिभार कहा जाएगा। इसके अलावा, इटरेटर को कास्टिंग करने के बजाए, आप टेम्पलेट प्रकार निर्दिष्ट कर सकते हैं: 'std :: distance (इनपुट.बेजिन(), itTarget)' –