एक इनपुट स्ट्रीम पर पुनरावृति करने के लिए, हम आम तौर पर तो जैसे एक std::istream_iterator
का प्रयोग करेंगे:रेंज आधारित एक इनपुट स्ट्रीम पर पाश
typedef std::istream_iterator<std::string> input_iterator;
std::ifstream file("myfile");
for (input_iterator i(file); i != input_iterator(); i++) {
// Here, *i denotes each element extracted from the file
}
यह अच्छा होगा यदि हम करने के लिए सीमा के आधार पर for
बयान का इस्तेमाल कर सकते हो जाएगा इनपुट धाराओं पर फिर से शुरू करें।
if
_RangeT
is an array type, begin-expr and end-expr are__range
and__range + __bound
, respectively, where__bound
is the array bound. If_RangeT
is an array of unknown size or an array of incomplete type, the program is ill-formed;if
_RangeT
is a class type, the unqualified-idsbegin
andend
are looked up in the scope of class_RangeT
as if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration, begin-expr and end-expr are__range.begin()
and__range.end()
, respectively;otherwise, begin-expr and end-expr are
begin(__range)
andend(__range)
, respectively, wherebegin
andend
are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespacestd
is an associated namespace.
इनपुट धाराओं इन सदस्य कार्यों की जरूरत नहीं है: हालांकि, वर्ग प्रकार की वस्तुओं के लिए, सीमा आधारित for
वस्तु begin()
और end()
सदस्य कार्य (§6.5.4, बोल्ड जोर जोड़ा) करने की आवश्यकता है (वे कंटेनर नहीं हैं) और इसलिए श्रेणी-आधारित for
उन पर काम नहीं करेगा। यह वैसे भी समझ में आता है क्योंकि उपरोक्त मामले में निकालने के लिए आपको निर्दिष्ट करने के लिए कुछ तरीका चाहिए (std::string
)।
लेकिन अगर हम जानते हैं कि हम निकालना चाहते हैं, यह संभव हमारे अपने begin()
और end()
कार्यों (शायद विशेषज्ञताओं या std::begin()
और std::end()
के भार के) इनपुट ऐसी धाराओं के लिए है कि वे के रूप में वर्ग के सदस्य पहुंच देखने के द्वारा पाया जा होगा परिभाषित करने के लिए है ऊपर वर्णित?
यह §6.5.4 से अस्पष्ट (कम से कम मेरे लिए) अस्पष्ट है यदि पिछले लुकअप विफल होने पर फ़ंक्शन को तर्क-निर्भर लुकअप के साथ देखा जाएगा या नहीं। विचार करने की एक और बात यह है कि std::ios_base
और इसके डेरिवेटिव्स के पास पहले से ही end
नामक सदस्य है जो मांगने के लिए एक ध्वज है।
std::ifstream file("myfile");
for (const std::string& str : file) {
// Here, str denotes each element extracted from the file
}
या::
std::ifstream file("myfile");
for (auto i = begin(file); i != end(file); i++) {
// Here, *i denotes each element extracted from the file
}
क्या यह सिर्फ मुझे है, या यह कल्पना से बहुत अस्पष्ट है? ऐसा लगता है कि 'std :: start()' और 'std :: end()' केवल तभी मिलेगा जब '_RangeT' सरणी या वर्ग प्रकार का नहीं है। –
हाँ, यह सबसे अच्छा शब्द नहीं है, लेकिन मुझे लगता है कि आप इसे पढ़ने के इरादे से हैं "यदि यह एक वर्ग है और इसमें है .begin और .end तो यह उन लोगों का उपयोग करेगा ... अन्यथा, यानी आप मुफ्त कार्यों को प्रदान कर सकते हैं । –
"*' start' और 'end' को कक्षा _ रेंजटी के दायरे में देखा जाता है ... और यदि ** या तो ... कम से कम एक घोषणा **, 'प्रारंभ-expr' और' end-expr' पाता है) '__range.begin()' और '__range.end()' * "- चूंकि' std :: ios_base :: end' मौजूद है (और इस प्रकार 'std :: ifstream :: end' मिलेगा) गेम ऊपर है । '.begin()' नहीं मिलेगा, और '.end()' एक वाक्यविन्यास त्रुटि होगी। –