日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

14.理解copy_if算法的正确实现

發(fā)布時(shí)間:2024/4/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 14.理解copy_if算法的正确实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

STL中有11個(gè)算法名字包含"copy":

copy
copy_backward
replace_copy
reverse_copy
replace_copy_if
unique_copy
remove_copy
rotate_copy
remove_copy_if
partial_sort_copy
unitialized_copy
但是STL中卻不包括copy_if的實(shí)現(xiàn),如果需要它,必須自己實(shí)現(xiàn)。
下面是copy_if的正確實(shí)現(xiàn):

template<typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator copy_if(InputIterator begin, InputIterator end, OutputIterator destBegin, Predicate p) {while (begin != end){if (p(*begin)) *destBegin++ == *begin;++begin;}return destBegin; }

下面是SGI STL中copy算法的實(shí)現(xiàn):

template <class _InputIter, class _OutputIter, class _Distance> inline _OutputIter __copy(_InputIter __first, _InputIter __last, _OutputIter __result, input_iterator_tag, _Distance*) {for (; __first != __last; ++__result, ++__first)*__result = *__first;return __result; }template <class _RandomAccessIter, class _OutputIter, class _Distance> inline _OutputIter __copy(_RandomAccessIter __first, _RandomAccessIter __last, _OutputIter __result, random_access_iterator_tag, _Distance*) {for (_Distance __n = __last - __first; __n > 0; --__n)?{*__result = *__first;++__first;++__result;}return __result; }

注意,copy、copy_if算法中都沒有對(duì)__result進(jìn)行有效性判斷(也不法進(jìn)行判斷), 所以調(diào)用方必須確保目標(biāo)位置__result有效,否則會(huì)出現(xiàn)未定義行為。

總結(jié)

以上是生活随笔為你收集整理的14.理解copy_if算法的正确实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。