STL中删除和替换算法 (15 个)

1. copy: 复制字符串

template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}

eg: 	const int N = 7;
	vector<int> ivec1( N, 2 );
        int Array[N] = { 1, 2, 3, 4, 5, 6, 7 };
        vector<int> ivec2;
        ivec2.resize(N);
        copy( Array, Array + N, ivec2.begin() );
        copy( ivec1.begin(), ivec.end(), ivec2.begin() );

2. copy_backward: 与copy相同, 不过元素是以相反序列被拷贝

template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result )
{
  while (last!=first) *(--result) = *(--last);
  return result;
}
eg:
	copy_backward( Array, Array + N, ivec2.end() );	//输出结果与上列中ivec2相同
 
3. iter_swap 交换两个ForwardIterator的值,可以交换不同/相同容器相同类型的指定位置的元素值
 
template <class ForwardIterator1, class ForwardIterator2>
  void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )
{
  swap (*a, *b);
}

eg:	iter_swap( ivec2.begin(), ivec2.end()); //结果: 7, 2, 3, 4, 5, 6, 1
	iter_swap( ivec1.begin(), ivec2.begin()); //结果: ivec1 7, 2, 2, 2, 2,2,2
								ivec2 2, 2, 3, 4, 5, 6,1

4. remove 删除指定范围内所有等于指定元素的元素
 
template < class ForwardIterator, class T >
  ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}

以上代码可以看出remove 不是真正的删除元素,返回的是指向第一个指定元素的迭代器
eg:
	
	int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20
 	 // bounds of range:
 	int* pbegin = myints;                          // ^
  	int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^

  	pend = remove (pbegin, pend, 20);              // 10 30 30 10 10 ?  ?  ?
                                                 // ^              ^
  	cout << "range contains:";
  	for (int* p=pbegin; p!=pend; ++p)
    		cout << " " << *p;

5. remove_copy :将所有不匹配的元素复制到一个指定容器中
 
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, const T& value )
{
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}

eg:
	
  vector<int> myvector (8);
  vector<int>::iterator it;

  remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0
 
6. remove_if 删除指定范围内输入操作结果为TRUE的所有元素
 
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}

7. remove_copy_if 将不匹配的元素拷贝到一个指定的容器里
 
template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                  OutputIterator result, Predicate pred )
{
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}

用法是remove copy if 功能的合成
 
8. replace 将指定范围所有等于void的元素都用Vnew代替
 
template < class ForwardIterator, class T >
  void replace ( ForwardIterator first, ForwardIterator last,
                 const T& old_value, const T& new_value )
{
  for (; first != last; ++first)
    if (*first == old_value) *first=new_value;
}

eg:
	vector<int> ivec(4, 2);
	replace( ivec.begin(), ivec.end(), 2, 3);   //用3 代替 2
9. replace_copy 替代加拷贝
template < class InputIterator, class OutputIterator, class T >
  OutputIterator replace_copy ( InputIterator first, InputIterator last,
                                OutputIterator result, const T& old_value, const T& new_value )
{
  for (; first != last; ++first, ++result)
    *result = (*first==old_value)? new_value: *first;
  return result;
}

eg:
	vector<int> ivec1;
	ivec1.resize(4);
	replace_copy( ivec.begin(), ivec.end(), ivec1.begin(), 2, 3);
		
10. replace_if 满足某种条件后在进行代替
template < class ForwardIterator, class Predicate, class T >
  void replace_if ( ForwardIterator first, ForwardIterator last,
                    Predicate pred, const T& new_value )
{
  for (; first != last; ++first)
    if (pred(*first)) *first=new_value;
}

eg:
	bool Fun( int i){ if( i < 3) return false;}
	replace_if( ievc.begin(), ivec.end(), Fun, 4);
11. replace_copy_if  以上两个函数的合成
template < class InputIterator, class OutputIterator, class Predicate, class T >
  OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
                                   OutputIterator result, Predicate pred,
                                   const T& new_value )
{
  for (; first != last; ++first, ++result)
    *result = (pred(*first))? new_value: *first;
  return result;
}

12 swap 交换存储在两个对象中的值
 
template <class T> void swap ( T& a, T& b )
{
  T c(a); a=b; b=c;
}

13. swap_range 在指定范围内的元素与另一个序列元素值进行交换
 
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                 ForwardIterator2 first2 )
{
  while (first1!=last1) swap(*first1++, *first2++);
  return first2;
}

用第二个容器中的元素(从first2开始,(last1 - frist1)个)与第一个迭代器相交换

14. unique 清除序列中重复的元素,和remove类似

template <class ForwardIterator>
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator result=first;
  while (++first != last)
  {
    if (!(*result == *first))  // or: if (!pred(*result,*first)) for the pred version
      *(++result)=*first;
  }
  return ++result;
}

15. unique_copy : 与unique类似, 并把结果输出到另一个容器中。

template <class InputIterator, class OutputIterator>
  OutputIterator unique_copy ( InputIterator first, InputIterator last,
                               OutputIterator result )
{
  typename std::iterator_traits<InputIterator>::value_type value = *first;
  *result=*first;
  while (++first != last)
  {
    if (!(value == *first))  // or: if (!pred(value,*first)) for the pred version
      *(++result) = value = *first;
  }
  return ++result;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值