泛型算法系列18:remove()&&remove_copy()
2009-08-15 14:25
477 查看
// remove.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <algorithm> #include <vector> #include <iostream> /* ハ莎・ original vector sequence: 0 1 0 2 0 3 0 4 0 5 vector after remove, without applying erase(): 1 2 3 4 5 3 0 4 0 5 vector after erase(): 1 2 3 4 5 array after remove_copy(): 1 2 3 4 5 */ template<class _II, class _Ty> inline _II my_find(_II _F, _II _L, const _Ty& _V) { for (; _F != _L; ++_F) if (*_F == _V) break; return (_F); } template<class _II, class _OI, class _Ty> inline _OI my_remove_copy(_II _F, _II _L, _OI _X, const _Ty& _V) { for (; _F != _L; ++_F) if (!(*_F == _V)) *_X++ = *_F; return (_X); } template<class _FI, class _Ty> inline _FI my_remove(_FI _F, _FI _L, const _Ty& _V) { _F = find(_F, _L, _V); if (_F == _L) return (_F); else { _FI _Fb = _F; return (my_remove_copy(++_F, _L, _Fb, _V)); } } using namespace std; void element_print(int val) { cout << val << " "; } int main(int argc, char* argv[]) { int value = 0; int ia[] = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5 }; vector< int > vec( ia, ia + 10 ); vector< int >::iterator vec_iter; cout << "original vector sequence:/n"; for_each(vec.begin(), vec.end(), element_print); cout << '/n'; vec_iter = my_remove( vec.begin(), vec.end(), value ); cout << "vector after remove, without applying erase():/n"; for_each(vec.begin(), vec.end(), element_print); cout << '/n'; // erase the invalid elements from container vec.erase( vec_iter, vec.end() ); cout << "vector after erase():/n"; for_each(vec.begin(), vec.end(), element_print); cout << '/n'; int ia2[5]; vector< int > vec2( ia, ia+10 ); my_remove_copy( vec2.begin(), vec2.end(), ia2, value ); cout << "array after remove_copy():/n"; for_each(vec.begin(), vec.end(), element_print); cout << '/n'; return 0; }
相关文章推荐
- 泛型算法系列19:remove_if()&&remove_copy_if()
- 泛型算法系列29:unique()&&unique_copy()
- 泛型算法系列20:reverse()&&reverse_copy()
- 泛型算法系列28:partial_sort()&&partial_sort_copy()&&partial_sum()
- 泛型算法系列32:replace()&&replace_copy()&&replace_if()&&replace_copy_if()
- 泛型算法系列21:retate()&&retate_copy()
- 泛型算法系列22:search()&&search_n()
- 泛型算法系列10:copy()&&copy_backward()
- 泛型算法系列12:fill()&&fill_n()
- STL常用算法: remove,remove_if,remove_copy,remove_copy_if,remove系列算法和partition的区别...
- DM9000网卡驱动源码分析系列03 - probe && remove
- 泛型算法系列5:lower_bound()&&upper_bound()
- 泛型算法系列17:generate_n()&&generate()
- 【量化小讲堂-Python&Pandas系列18】平均趋向指标(ADX)策略在A股的实证
- 泛型算法系列33:merge()&&inplace_merge()
- 泛型算法系列35:set_difference()&&set_intersection()&&set_symmetric_difference()&&set_union()
- 分布式存储系统可靠性系列五:副本放置算法 & CopySet Replication
- 泛型算法系列27:next_permutation()&&prev_permutation()
- 分布式存储系统可靠性系列五:副本放置算法 & CopySet Replication
- 泛型算法系列30:stable_partition()&amp;&amp;partition()&&stable_sort()