當前位置:首頁 » 操作系統 » 演算法end

演算法end

發布時間: 2024-05-10 19:39:13

演算法怎麼寫

演算法的流程書寫可通過流程圖或偽代碼來完成。

所謂流程圖是指以特定的圖形符號加上說讓譽明,表示演算法的圖,用它來表示演算法思路是一種極燃滑蘆好的方法,因為有時候千言萬語不如一張圖形象生動易於理解,例如:

而偽代碼是介於自然語言和計算機語言之間的文字和符號(包括數學符號),它是一種不依賴於語言、用來表示程序執行過程、而不一定能皮帶編譯運行的代碼,例如:

Begin(演算法開始)

輸入 A,B,C

IF A>B 則 A→Max

否則 B→Max

IF C>Max 則 C→Max

Print Max

End (演算法結束)

Ⅱ c++鐨凷TL瀹瑰櫒涓鐨別nd()鍏蜂綋鏈哄埗鏄鎬庝箞鏍

STL涓涓烘垜浠鎻愪緵鐨勬渶閲嶈佺殑涓や釜鍐呭規槸瀹瑰櫒錛坴ector錛宭ist絳夛級鍜屼竴緋誨垪鐨勭畻娉曘傚湪榪欎簺綆楁硶涓鏈夎稿氶渶瑕侀亶鍘嗗瑰櫒涓鐨勬墍鏈夊厓緔狅紝濡俿earch錛宻ort絳夌畻娉曘係TL鐨勮捐¤呭笇鏈涘皢綆楁硶鍜屽瑰櫒鍒嗙誨紑鏉ワ紝涓涓綆楁硶鍙浠ュ府涓嶅悓鐨勫瑰櫒瀹炵幇鍔熻兘銆備負姝ょ洰鐨勶紝STL搴旂敤鍒頒簡銆婅捐℃ā寮忋嬩腑鎻愬埌鐨勮凱浠e櫒妯″紡銆
銆婅捐℃ā寮忋嬩腑灝嗚凱浠e櫒妯″紡瀹氫箟涓猴細鎻愪緵涓縐嶆柟娉曪紝浣誇箣鑳戒緷搴忛亶鍘嗗瑰櫒涓鐨勬瘡涓鍏冪礌錛岃屼笉闇瑕佹毚闇插瑰櫒鍐呴儴鐨勪俊鎮銆傚湪闈㈠悜瀵硅薄鐨勮捐¢噷錛屽疄鐜拌凱浠e櫒錛屾垜浠浼氫負鎵鏈夌殑榪浠e櫒鎻愪緵涓涓緇熶竴鐨勬帴鍙o紝鐒跺悗鏇翠釜瀹瑰櫒鐨勮凱浠e櫒緇ф壙榪欎釜鎺ュ彛錛岀劧鍚庡湪浣跨敤澶勯氳繃鎺ュ彛鏉ュ疄鐜板逛笉鍚岃凱浠e櫒鐨勮皟鐢ㄣ備絾STL鏄閲囩敤娉涘瀷鎬濈淮錛屽埄鐢ㄦ懜鐗堟潵瀹炵幇鐨勭被搴擄紝鎵浠ュ畠鐨勮凱浠e櫒鐨勫疄鐜板拰闈㈠悜瀵硅薄紼嶆湁涓嶅悓錛屼絾鍘熺悊綾諱技銆傚湪姝ゅ皢閫氳繃渚嬪瓙鏉ュ叿浣撶湅鐪婼TL涓榪浠e櫒鐨勫疄鐜般
鎴戜滑鍏堟潵鐪嬬湅find()鍑芥暟鐨勫疄鐜幫細
//鎽樿嚜stl_algo.h
template<typename _InputIter, typename _Tp>
inline _InputIter
find(_InputIter __first, _InputIter __last,
const _Tp& __val,
input_iterator_tag)
{
while (__first != __last && !(*__first == __val))
++__first;
return __first;
}

find()鍑芥暟鐨勫姛鑳芥槸鍦ㄤ竴涓瀹瑰櫒涓鏌ユ壘涓涓鍏冪礌錛岄渶瑕侀亶鍘嗕袱涓榪浠e櫒瀵硅薄_first鍜宊last鎵鍦ㄧ殑鍖洪棿銆傚湪姝ゆ垜浠鐪嬪埌瀹冨瑰尯闂寸殑閬嶅巻鏄閫氳繃++__first鏉ュ疄鐜扮殑銆備篃灝辨槸璇磋凱浠e櫒闇瑕侀噸杞++榪愮畻絎︽潵瀹炵幇瀵瑰瑰櫒鐨勯亶鍘嗐傛潵鍏蜂綋鐪嬬湅vector鍜宭ist涓鐨勮凱浠e櫒瀹炵幇銆
鎴戜滑鐭ラ亾vector涓鐨勫厓緔犳槸榪炵畫瀛樻斁鐨勶紝綾諱技浜庢暟緇勶紝鍦ㄦ暟緇勪腑鍙浠ラ氳繃鎸囬拡寰堝規槗鐨勫疄鐜板規暟緇勭殑閬嶅巻錛屽湪vector涓涔熶竴鏍鳳紝騫朵笉闇瑕佹彁渚涚粰瀹冧笓闂ㄧ殑榪浠e櫒綾伙紝閫氳繃鎸囬拡灝卞彲浠ュ疄鐜拌凱浠e櫒鐨勫姛鑳姐傜湅鐪嬩緥瀛愶細
#include<iostream>
#include<vector>
using namespace std;

template<typename _InputIter, typename _Tp>
inline _InputIter
myfind(_InputIter __first, _InputIter __last,
const _Tp& __val)
{
while (__first != __last && !(*__first == __val))
++__first;
return __first;
}

int main()
{
vector<int> vec;

for( int i = 0; i < 10 ; i ++ )
vec.push_back(i);

int *p0 = &vec[0];
int *p1 = &vec[10];

int *p2 = myfind(p0,p1,6);

if ( p2 != &vec[10])
cout << *p2 << endl;

system("pause");
return 0;
}
渚嬪瓙涓錛屾垜瀵筬ind鍋氫簡鏀瑰姩錛屾敼鎴愪簡myfind錛岃繖鏄鍥犱負3.3.1鐨勬簮浠g爜涓鍔犲叆浜唗raits鎶宸э紙鍦╒C涓鎴戜滑鏄鍙浠ョ洿鎺ョ敤find鐨勶級錛岀◢鍚庢垜浠灝嗚插埌璇ラ棶棰樸備絾榪欓噷鍙浠ョ湅鍒幫紝閫氳繃鎸囬拡灝卞彲浠ュ疄鐜板箆ector鐨勯亶鍘嗐備竴鑸鎴戜滑瀵硅凱浠e櫒鐨勪嬌鐢ㄦ槸濡備笅鐨勬柟寮忥細
vector<int>::iterator it;
榪欓噷闇瑕佸熷姪vector鏉ュ畾涔夎凱浠e櫒錛岃繖閲屼富瑕佹槸涓哄瑰櫒鐨勪嬌鐢ㄦ彁渚涗竴縐嶇粺涓鐨勬柟寮忋傚洜涓簂ist瀹瑰櫒涓嶈兘閫氳繃鎸囬拡鏉ュ疄鐜伴亶鍘嗭紝瀹冧滑闇瑕佸疄鐜拌嚜宸辯殑榪浠e櫒銆傛垜浠鏉ョ湅鐪嬪湪vector涓瀵逛簬iterator鐨勫疄鐜幫細
template<typename T,class Alloc = alloc >
class vector
{
public:
typedef T value_type;
typedef value_type* iterator;

...
};
鍦ㄦゅ彲浠ョ湅鍒癷terator鍦╲ector涓涔熷彧鏄綆鍗曠殑琚瀹氫箟鎴愪簡鎴戜滑浼犲叆鐨勭被鍨嬪弬鏁癟鐨勬寚閽堬紙鍦3.3.1鐨勪唬鐮佷腑涓庤繖閲岀殑浠g爜騫朵笉涓鏍鳳紝榪樻槸traits鐨勫師鍥狅級銆傛垜浠浣跨敤榪浠e櫒鐨勪竴鑸鏂瑰紡鏄濡備笅錛
find(vec.begin(),vec.end,6);
鍐嶇畝鍗曠湅鐪媌egin鍜宔nd鍑芥暟鐨勫疄鐜幫細
template<typename T,class Alloc = alloc >
class vector
{
proteted:
iterator start;
iterator finist;
public:
iterator begin(){return start;}
iterator end(){return end;}
...
};
榪欓噷鍙浠ョ湅鍒幫紝鍙鏄涓簐ector璁劇疆涓涓澶存寚閽堝拰灝炬寚閽堬紝鐒跺悗閫氳繃begin鍜宔nd鏉ュ緱鍒拌繖涓や釜鎸囬拡銆
list灝變笉鍙浠ョ洿鎺ラ氳繃鎸囬拡鏉ュ疄鐜拌凱浠e櫒浜嗭紝鍥犱負瀹冮噷闈㈢殑鍏冪礌鏄涓嶈繛緇鐨勩傞渶瑕佹垜浠鑷宸卞幓瀹炵幇錛屾垜浠鍏堟妸list鐨勫畾涔夋娊鍑烘潵鐪嬬湅銆
template <class _Tp >
struct _List_node {
_List_node< _Tp>* _M_next;
_List_node< _Tp>* _M_prev;
_Tp _M_data;
};
template <class _Tp, class Alloc = alloc>
class list {
protected:
_List_nod<_Tp>e* node;
....
public:
void push_front(const _Tp & x);
void push_back(const _Tp & x);
....
};
涓轟簡渚誇簬鐞嗚В錛屾垜灝嗕唬鐮佽繘琛屼簡綆鍗曠殑淇鏀癸紝浜嬪疄涓3.3.1閲岀殑浠g爜瑕佹瘮榪欎釜瑕佸嶆潅涓浜涖備竴鑸鏉ヨ磋捐′竴涓閾捐〃錛屾垜浠浼氭妸瀹冨垎鎴愪袱閮ㄥ垎錛歘List_node鐢ㄦ潵瀛樻斁鏁版嵁錛宭ist鐢ㄦ潵鎿嶄綔閾捐〃銆傝繖鏍鳳紝榪浠e櫒瀹為檯浼犲洖鐨勫簲璇ユ槸_List_node錛屽綋瀹冨悜鍓嶉亶鍘嗙殑鏃跺欙紝浼犲洖鐨勫簲璇ユ槸瀹冪殑涓嬩竴涓緇撶偣銆傛寜鐫榪欎釜緇撴瀯錛屼負閾捐〃鏉ヨ捐′竴涓榪浠e櫒錛屽備笅錛
template<class T>
struct __list_iterator {
_List_node<T>* node;
__list_iterator(_List_node<T>* x) : node(x) {}
__list_iterator() {}

bool operator==(const __list_iterator<T>& x) const { return node == x.node; }
bool operator!=(const __list_iterator<T>& x) const { return node != x.node; }

T& operator*() const { return (*node)._M_data; }

__list_iterator<T>& operator++() {
node = (_List_node<T>*)((*node)._M_next);
return *this;
}

__list_iterator<T> operator++(int) {
__list_iterator<T> tmp = *this;
++*this;
return tmp;
}

};
鎴戜滑鍙浠ョ湅鍒幫紝榪浠e櫒涓閲嶈澆浜==錛岋紒=錛*錛++絳夋搷浣滅︼紝榪欐槸鍥犱負榪欎簺鎿嶄綔鏄鍦ㄧ畻娉曚腑緇忓父瑕佺敤鍒扮殑鎿嶄綔絎︺傜湅鐪媘yfind涓瀹炵幇錛屽氨鍙浠ユ槑鐧斤細
while (__first != __last && !(*__first == __val))
++__first;
榪欓噷瑕佺敤鍒頒袱涓榪浠e櫒鐨勬瘮杈冿紙__first != __last錛夛紝鎵浠ユ垜浠闇瑕侀噸杞界瓑浜庤繍綆楃︼紱榪浠e櫒涓鍊肩殑姣旇緝錛*__first == __val錛夛紝榪欐椂鍊欐瘮杈冪殑涓嶅啀鏄榪浠e櫒錛岃屾槸榪浠e櫒鎵鍖呭惈鐨勫唴瀹癸紙榪欓噷鏄痏List_node鐨勫硅薄錛夛紝鎵浠ユ垜浠闇瑕侀噸杞*鏉ュ緱鍒板畠錛涙渶閲嶈佺殑灝辨槸閬嶅巻鐨勫疄鐜幫紝闇瑕侀噸杞++榪愮畻絎︼紝榪欓噷鏄閫氳繃node = (_List_node<T>*)((*node)._M_next);灝嗚凱浠e櫒涓鐨刜List_node瀵硅薄鏀逛負瀹冪殑涓嬩竴涓瀵硅薄鏉ュ疄鐜扮殑銆傝繖鏍鋒垜浠灝卞畬鎴愪簡榪浠e櫒鐨勮捐°傚啀鏉ョ湅鐪嬪畠鐨勭敤娉曪細
int main(int argc, char *argv[])
{
list<int> mylist;

for(int i = 0;i <= 10; i++ )
{
mylist.push_back(i);
}
// mylist.begin()寰楀埌鐨勬槸STL鏈韜鎻愪緵鐨勮凱浠e櫒錛屾墍浠ユ垜浠闇瑕佸緱鍒板畠鎵鍖呭惈鐨刜List_node<T>瀵硅薄鎸囬拡銆
_List_node<int>* p1 = (_List_node<int>*)mylist.begin()._M_node;
_List_node<int>* p2 = (_List_node<int>*)mylist.end()._M_node;

//榪欓噷鐢ㄦ垜浠寰楀埌鐨勬寚閽堟潵鐢熸垚鎴戜滑鑷宸辯殑榪浠e櫒
__list_iterator<int> it1(p1);
__list_iterator<int> it2(p2);

__list_iterator<int> it3 = myfind(it1,it2,6);

if ( it3 != it2 )
cout << *it3 << endl;

system("PAUSE");
return 0;
}
瀹為檯鐨勮凱浠e櫒鐨勮捐′腑錛岃繕浼氶噸杞--鍜->榪愮畻絎︼紝鎰熷叴瓚g殑鍙浠ヨ嚜宸辯湅鐪嬫簮浠g爜銆傝繕鏄鍜寁ector涓鏍鳳紝STL浼氭妸榪浠e櫒鐨勫畾涔夌粦瀹氬埌list綾諱腑鍘伙紝鎴戜滑鏉ョ湅鐪嬪畠鐨勫叿浣撳仛娉曪細
typedef _list_iterator<_Tp> iterator;
鍏跺疄鍜寁ector涓鏍鳳紝鍙瑕佹妸浠ヤ笂鐨勫畾涔夋斁鍒發ist綾諱腑錛屽氨鍙浠ヤ簡錛岀劧鍚庯紝鎴戜滑灝卞彲浠ラ氳繃list<int>::iterator ite 鏉ュ畾涔塴ist鐨勮凱浠e櫒浜嗭紝浜嬪疄涓婂畠鐢熸垚鐨勫氨鏄涓涓猒list_iterator<int>瀵硅薄銆
浠庢ゅ彲浠ョ湅鍒拌凱浠e櫒灝辨槸涓哄悇涓瀹瑰櫒鐢熸垚涓涓鍙浠ヨ凱浠h嚜韜鐨勭被錛岃屽啀閫氳繃typedef鏉ユ妸璇ヨ凱浠g被緇戝畾鍒板瑰櫒綾諱腑錛岃繖鏍峰氨緇欎簡澶栭儴涓涓褰㈠紡緇熶竴鐨勪嬌鐢ㄦ柟娉曘
闄や簡閬嶅巻錛岃凱浠e櫒涓榪樼敤鍒頒簡涓涓閲嶈佺殑鎶宸р斺攖raits銆傛垜浠鍏堟潵鐪嬬湅traits鐨勭敤澶勩

template <class ForwardIterator1, class ForwardIterator2, class T>
inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, T*) {
T tmp = *a;
*a = *b;
*b = tmp;
}
璇ユ柟娉曟槸鐢ㄦ潵瀹炵幇涓や釜榪浠e櫒涓鍐呭圭殑浜ゆ崲錛屾垜浠鍙浠ョ湅鍒版ゅ勯渶瑕佷竴涓涓存椂鍙橀噺tmp錛岃宼mp鎵瀵瑰簲鐨勭被鍨嬫槸瀹冩墍鍖呭惈鐨勫硅薄鐨勭被鍨嬨傝繖鏍鋒垜浠灝遍渶瑕佷紶鍏ヤ竴涓綾誨瀷鍙傛暟T*榪涘幓錛屽傛灉鎴戜滑涓嶆兂浼犺繖涓鍙傛暟榪涘幓鍙涓嶅彲浠ュ憿錛熻繖鏃跺欏氨闇瑕佺敤鍒皌raits鎶宸т簡銆備簨瀹炰笂traits騫舵病鏈変粈涔堝お闅劇悊瑙g殑閮ㄥ垎錛岃繕鏄鐪嬬湅渚嬪瓙錛
template<class T>
struct __list_iterator {
typedef T value_type;
鈥.
};
鐜板湪鎴戜滑鍦╛_list_iterator澶撮儴鍔爐ypedef T value_type;榪欎箞涓鍙ヨ瘽錛岀劧鍚庢垜浠閲嶆柊鍐欎竴涓嬭凱浠e櫒鐨勪氦鎹㈠嚱鏁幫紝濡備笅錛
template <class ForwardIterator1, class ForwardIterator2>
inline void myiter_swap(ForwardIterator1 a, ForwardIterator2 b) {
typename ForwardIterator1::value_type tmp = *a;
*a = *b;
*b = tmp;
}
姝ゆ椂錛屽氨鍙浠ラ氳繃typename ForwardIterator1::value_type鏉ュ畾涔変復鏃跺彉閲忎簡錛岀湅鐪嬪畠鐨勬晥鏋滐細
int main(int argc, char *argv[])
{
list<int> mylist;

for(int i = 0;i <= 10; i++ )
{
mylist.push_back(i);
}
_List_node<int>* p1 = (_List_node<int>*)mylist.begin()._M_node;
_List_node<int>* p2 = (_List_node<int>*)mylist.end()._M_node;

__list_iterator<int> it1(p1);
__list_iterator<int> it2(p2);

__list_iterator<int> it3(p2);
//浜ゆ崲絎涓涓鍜屾渶鍚庝竴涓鍏冪礌鐨勫唴瀹
myiter_swap(it1,--it3);
//鎵撳嵃鎵鏈夌殑鍏冪礌
for( ;it1 != it2;it1++)
cout << *it1 << endl;
system("PAUSE");
return 0;
}
浜嬪疄涓婏紝鍦ㄨ凱浠e櫒涓榪樹細鍖呮嫭鎸囬拡錛屽紩鐢錛岃繕鏈夊氨鏄涓や釜榪浠e櫒涔嬮棿璺濈葷殑綾誨瀷銆傛墍浠ヨ凱浠e櫒鐨勪腑鐨勫疄闄呬唬鐮佸簲璇ュ備笅錛
template<class T>
struct __list_iterator {
typedef bidirectional_iterator_tag iterator_category; //榪浠e櫒鐨勭被鍨嬶紝紼嶅悗瑙i噴
typedef T value_type; // 瀵硅薄綾誨瀷
typedef value_type*pointer; // 鎸囬拡綾誨瀷
typedef value_type& reference; // 寮曠敤綾誨瀷
typedef ptrdiff_t difference_type; // 榪浠e櫒璺濈葷被鍨嬶紝鍏充簬榪欎釜鍙傛暟錛屾垜涔熸悶涓嶆槑鐧戒負浠涔堥渶瑕

};
榪欐牱鍏蜂綋鐨勭畻娉曚腑灝卞彲浠ユ牴鎹榪欎簺鍐呭規潵瀹氫箟鑷宸辨墍闇瑕佺殑綾誨瀷浜嗭紙STL鐨勫疄闄呭畾涔変腑鍜岃繖閲屾湁浜涗笉鍚岋紝鍦ㄥ瑰櫒綾伙紙vector,list錛変腑宸茬粡瀹氫箟浜嗚繖浜涚被鍨嬶紝榪浠e櫒鏄鍐嶉氳繃瀹瑰櫒綾誨畾涔夛紝涓嶈繃鍘熺悊鏄涓鑷寸殑錛夈傚彟澶栵紝STL涓榪樻彁渚涗簡涓涓鑷宸辯殑钀冨彇鏈猴紝濡備笅錛
template<typename _Iterator>
struct iterator_traits {
typedef typename _Iterator::iterator_category iterator_category;
typedef typename _Iterator::value_type value_type;
typedef typename _Iterator::difference_type difference_type;
typedef typename _Iterator::pointer pointer;
typedef typename _Iterator::reference reference;
};
鏈変簡瀹冿紝鎴戜滑鐨刴yinter_swap鍑芥暟灝卞彲浠ュ彉鎴愪互涓嬬殑鍐呭癸細
template <class ForwardIterator1, class ForwardIterator2>
inline void myiter_swap(ForwardIterator1 a, ForwardIterator2 b) {
typedef typename iterator_traits<ForwardIterator1>::value_type _ValueType1;
_ValueType1 tmp = *a;
*a = *b;
*b = tmp;
}

浼間箮榪欐典唬鐮佸苟娌℃湁浣誇唬鐮佸彉鐨勬柟渚匡紵鎯蟲兂錛屾垜浠鐨剉ector涓騫舵病鏈夊畾涔夎凱浠e櫒綾伙紝鍙鏄鐩存帴鎶婃寚閽堝綋浣滀簡榪浠e櫒銆傚傛灉鐩存帴鐢ㄤ互涓婄殑浠g爜鏄庢樉鏄浼氬嚭闂棰樼殑錛屽洜涓烘寚閽堟牴鏈娌℃湁value_type榪欎簺淇℃伅銆備負浜嗚繖縐嶆儏鍐碉紝STL涓鎻愪緵浜嗚悆鍙栨満涓閽堝規寚閽堢殑鍋忕壒鍖栫増鏈錛

template<typename _Tp>
struct iterator_traits<_Tp*> {
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef _Tp& reference;
};
榪欐牱vector瀵硅薄灝卞彲浠ヤ嬌鐢╩yiter_swap浜嗭紝涓嶇劧鑲瀹氱紪璇戜笉榪囧幓銆傝繕鏈変竴浜涢拡瀵瑰叾瀹冪被鍨嬶紙濡傚父閲忔寚閽堬級鐨勫嚲鐗瑰寲鐗堟湰錛屽彲浠ヨ嚜宸辯湅鐪嬫簮鐮併
鍦ㄤ互涓婄殑浠g爜涓錛屾垜浠鐪嬪埌浜唗ypedef random_access_iterator_tag iterator_category;榪欐牱鐨勮鍙ャ俰terator_category浠h〃鐨勬槸榪浠e櫒鐨勫垎綾伙紝榪浠e櫒鍒嗕負浜旂嶇被鍨嬶細
1錛 Input Iterator 榪欑嶇被鍨嬬殑榪浠e櫒涓嶅厑璁稿栫晫鏀瑰彉錛屾槸鍞璇葷殑銆
2錛 Output Iterator 鏄鍞鍐欑殑銆
3錛 Forard Iterator 鍙浠ヨ誨啓錛屼絾瀹冨彧鑳藉悜鍓嶇Щ鍔ㄣ
4錛 Bidirectional Iterator 鍙浠ヨ誨啓錛屽彲浠ュ墠鍚庣Щ鍔ㄣ
5錛 Random Access Iterator 鍙浠ヨ誨啓錛屽墠鍚庣Щ鍔錛屽苟涓旀敮鎸乸+n鍨嬬殑鎿嶄綔錛屼篃灝辨槸璇村彲浠ュ墠鍚庣Щ鍔ㄥ氫綅錛岃屼笉鏄涓涓涓涓鐨勭Щ鍔ㄣ
鍦⊿TL涓錛屽畠浠琚瀹氫箟涓哄備笅鐨勫艦寮忥細
/// Marking input iterators.
struct input_iterator_tag {};
/// Marking output iterators.
struct output_iterator_tag {};
/// Forward iterators support a superset of input iterator operations.
struct forward_iterator_tag : public input_iterator_tag {};
/// Bidirectional iterators support a superset of forward iterator operations.
struct bidirectional_iterator_tag : public forward_iterator_tag {};
/// Random-access iterators support a superset of bidirectional iterator operations.
struct random_access_iterator_tag : public bidirectional_iterator_tag {};

鍙浠ョ湅鍒板畠浠騫舵病鏈夊疄闄呯殑浠g爜錛屽彧鏄浣滀負涓涓鏍囪版潵鐢ㄧ殑銆傚叿浣撲綔鐢ㄧ殑璇濓紝榪樻槸閫氳繃鍑芥暟鏉ョ湅鐪嬨係TL鐨勭畻娉曚腑鎻愪緵浜嗚繖鏍蜂竴涓鍑芥暟distance錛屽畠鐨勪綔鐢ㄦ槸奼傚嚭涓や釜榪浠e櫒涔嬮棿鐨勮窛紱伙紝涓轟簡緇熶竴錛屽嚱鏁板彲鑳藉啓鎴愯繖鏍鳳細
template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
__distance(_InputIterator __first, _InputIterator __last)
{
typename iterator_traits<_InputIterator>::difference_type __n = 0;
while (__first != __last) {
++__first; ++__n;
}
return __n;
}
榪欐牱鐨勮瘽錛寁ector鍜宭ist閮藉彲浠ヤ嬌鐢ㄨ繖涓鍑芥暟錛屼絾榪欐牱涓鏉ワ紝vector涔熼渶瑕侀亶鍘嗕袱涓榪浠e櫒涔嬮棿鐨勬墍鏈夊唴瀹逛簡銆備絾浜嬪疄涓婏紝鎴戜滑鐭ラ亾vector鏄榪炵畫鐨勫尯闂達紝瀹屽叏鍙浠ラ氳繃涓や釜榪浠e櫒涔嬮棿鐨勭浉鍑忔搷浣滄潵瀹屾垚榪欎釜璁$畻銆傚傛灉閲囩敤浠ヤ笂鐨勬搷浣滐紝浼氶檷浣庡畠鐨勬晥鐜囥傛湁浠涔堝姙娉曟潵鍖哄垎榪欎袱縐嶆搷浣滃憿錛熼氳繃榪浠e櫒綾誨瀷灝卞彲浠ュ畬鎴愶紝榪樻槸鐪嬬湅浠g爜錛
template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
__distance(_InputIterator __first, _InputIterator __last,
input_iterator_tag)
{
typename iterator_traits<_InputIterator>::difference_type __n = 0;
while (__first != __last) {
++__first; ++__n;
}
return __n;
}

template<typename _RandomAccessIterator>
inline typename iterator_traits<_RandomAccessIterator>::difference_type
__distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
random_access_iterator_tag)
{
return __last - __first;
}
template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
distance(_InputIterator __first, _InputIterator __last)
{
typedef typename iterator_traits<_InputIterator>::iterator_category category;
return __distance(__first, __last, category ());
}
鍙浠ョ湅鍒幫紝STL鎻愪緵浜哶_distance鐨勪袱涓閲嶈澆鍑芥暟錛氫竴涓閽堝筰nput_iterator_tag錛屼嬌鐢ㄧ殑鎴戜滑涓婇潰鎻愬埌鐨勯亶鍘嗙殑鏂規硶錛涗竴涓閽堝箁andom_access_iterator_tag錛岄噰鐢ㄧ殑鐩存帴鐩稿噺鐨勬柟娉曘傚苟鎻愪緵浜哾istance鍑芥暟錛屽湪璇ュ嚱鏁伴噷閫氳繃
typedef typename iterator_traits<_InputIterator>::iterator_category category;鏉ュ緱鍒拌凱浠e櫒鐨勭被鍨嬶紝騫墮氳繃category ()鐢熸垚涓涓瀵硅薄浼犲叆鍒癬_distance涓錛岀粡榪嘋++鐨勯噸杞芥満鍒跺疄鐜頒簡涓嶅悓綾誨瀷鐨勮皟鐢ㄣ

熱點內容
java中立方 發布:2024-11-27 07:07:05 瀏覽:484
編程Q1 發布:2024-11-27 06:52:29 瀏覽:459
版本編譯驗證 發布:2024-11-27 06:46:43 瀏覽:938
ftp上傳占內存 發布:2024-11-27 06:46:40 瀏覽:58
怎麼建java基岩互通伺服器 發布:2024-11-27 06:37:28 瀏覽:959
aspcms圖片上傳 發布:2024-11-27 06:32:20 瀏覽:416
qq空間本地上傳的音樂 發布:2024-11-27 06:14:50 瀏覽:921
辦公室雲電腦伺服器 發布:2024-11-27 06:11:45 瀏覽:27
有趣的php 發布:2024-11-27 05:58:13 瀏覽:960
php網頁開發 發布:2024-11-27 05:56:09 瀏覽:956