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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ stl 通用算法和成员函数使用

發(fā)布時間:2025/3/13 c/c++ 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ stl 通用算法和成员函数使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在stl中既有通用函數(shù),又有相同成員函數(shù)主要表現(xiàn)在list中。

以remove為例

list<int> coll;// insert elements from 6 to 1 and 1 to 6for (int i=1; i<=6; ++i) {coll.push_front(i);coll.push_back(i);}// print all elements of the collectioncout << "pre: ";copy (coll.cbegin(), coll.cend(), // sourceostream_iterator<int>(cout," ")); // destinationcout << endl;// remove all elements with value 3remove (coll.begin(), coll.end(), // range3); 開始是list中的元素為6 5 4 3 2 1 1 2 3 4 5 6

remove之后變成6 5 4 2 1 2 4 5 6 5 6

remove算法具有l(wèi)inear complexity是將容器中的等于value的值得元素使用后續(xù)的元素進行替換,這就需要進行deference。因此只是調(diào)整元素的位置,而不是真正的刪除。

函數(shù)返回的是A forward iterator addressing the new end position of the modified range, one past the final element of the remnant sequence free of the specified value

然而在list中也提供了一個remove的成員函數(shù),他是直接刪除元素。它刪除元素不需要移動元素,只需要進行相應(yīng)的指針操作。因此具有更好的性能。

template <class _Tp, class _Alloc> void list<_Tp, _Alloc>::remove(const _Tp& __value) {iterator __first = begin();iterator __last = end();while (__first != __last) {iterator __next = __first;++__next;if (*__first == __value) erase(__first);__first = __next;} }

所以在選擇通用算法和成員函數(shù)時,比較具有g(shù)ood performance(具有constant or linear complexity)。

轉(zhuǎn)載于:https://www.cnblogs.com/xuning2516/archive/2013/03/31/3018749.html

總結(jié)

以上是生活随笔為你收集整理的C++ stl 通用算法和成员函数使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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