当前位置:首页 » 操作系统 » 算法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涓锛岀粡杩嘋++镄勯吨杞芥満鍒跺疄鐜颁简涓嶅悓绫诲瀷镄勮皟鐢ㄣ

热点内容
linux重装linux 发布:2024-11-27 09:46:25 浏览:558
电脑玩云服务器 发布:2024-11-27 09:19:22 浏览:66
苹果什么助手能和安卓互通 发布:2024-11-27 09:18:47 浏览:58
android花屏 发布:2024-11-27 09:18:38 浏览:125
phpnginx负载均衡 发布:2024-11-27 09:18:36 浏览:623
刀片服务器如何设置网络交换机 发布:2024-11-27 09:17:23 浏览:476
eve脚本挖矿 发布:2024-11-27 09:12:25 浏览:566
模板平方算法 发布:2024-11-27 08:56:15 浏览:269
系统缓存文件 发布:2024-11-27 08:52:15 浏览:913
40乙腈水溶液怎么配置 发布:2024-11-27 08:52:03 浏览:64