C++:STL标准库:算法:更易型算法

STL算法

  • 1:更易型算法(Modifying Algorithm)
  • 1.1 :for_each()
    • 1.2 :taransform()算法
    • 1.2:copy() , copy_if() 算法

扼要介绍
STL算法采用 覆盖override模式而非 安插insert 模式,所以调用者必须保证标的区间拥有足够的空间。

1:更易型算法(Modifying Algorithm)

更易型算法的特点:

  • 或者直接改变元素值。
  • 或者在复制元素到另一区间的过程中改变元素值,这种情况下,原区间不会发生变化。
  • 最基本的更易型算法是 : for_each() 和 transform(),两者都可以改变序列中的所有元素值。

1.1 :for_each()

接受一个操作,该操作可以改动其所接受的实参,所以该实参必须以 by reference方式传递。

void square(int& elem)   // call by reference
{
	elem = elem * elem;  // assign processed value directly
}
for_each(coll.begin(), coll.end(), square);

for_each源码分析

function template
std::for_each

template <class InputIterator,class Function>
for_each(InputIterator first,InputIterator last,Function fn)

Apply function to range
Applies function fn to each of elements in the range [first,last)

The behavior of this template function is equivalent to
原型
template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator lasr, Function fn)
{
	while(first != last)
	{
		fn(*first);
		++first;
    }
    return fn;
}
参数详解
first ,last
The Range used is [First, last), which contains all the elements between first and last.

fn
 Unary function that accepts an element in the range as argument
 this can either be a function pointer or a move constructible function object
 its return value, if any ,is ignored.

1.2 :taransform()算法

transform()算法:运用某个操作,该操作返回被改动后的实参,此算法的奥妙之初就在于它可以用来将结果赋值给原元素。

我们以一元操作符为示例
int squqre(int elem)  // call-by-value
{
	return elem*elem;  // return processed value
}

示例
trasnform(coll.begin(), coll.end(), // source range
             coll.begin(),  // destination range
             square);  // operation
源码分析
function template
std::tradnsform

一元操作符
template<calss InputIterator, class OutputIterator,class UnaryOperator>
OutoutIterator transform(InputIterator first1, InputIterator last1,OutputIterator result,UnaryOperator op)
{
	while(first1 != last1)
	{
		// 一元操作符
		*result = op(*first);
		++result;
		++first;
    }
    return result;
}

二元操作符
template<calss InputIterator, calss InputIterator2 ,class OutputIterator,class UnaryOperator>
OutoutIterator transform(InputIterator first1, InputIterator last1,InputIterator2 first2 
		OutputIterator result,BinaryOperation binary_op)
{
	while(first1 != last1)
	{
		// 二元操作符
		*result = binary_op(*first1,*first2);
		++result;
		++first;
    }
    return result;
}
  • 一元操作符
  • 主要实现:从一个空间copy 元素到另一个空间
unary operation(1)
template<class InputIterator,class OutputIterator,class UnaryOperation>
OutputIterator transform(InoutIterator first1,InoutIterator last1,OutputIterator result,UnaryOperation op);

Applies op object to each of the elements in the range [first1,last1) and stores the value returned by each operation in the range that begins at result.
  • 二元操作符
  • 主要实现:将两个空间的元素组合起来,复制到另一个空间
binary operation(2)
template<class InputIterator1,calss InputIterator2,class OutputIterator,class BinaryOperation>
OutputIterator transform(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2, OutputIterator result , BinaryOperation binary_op)


Calls binary_op using each of the elements in the range [first1,last1) as first argument, and the respective argument in the range that begins at first2 as second atgument. the value returned by each call is stored in the range that begins at result.


案例分析:

#include   
#include   
#include   
using namespace std;  
  
int op_increase (int i) { return ++i;}  
int op_sum (int i, int j) { return i+j; }  
  
int main () {  
  vector<int> first;  
  vector<int> second;  
  vector<int>::iterator it;  
  
  // set some values:  
  for (int i=1; i<6; i++) first.push_back (i*10); 
  
  cout << "first contains:";  
  for (it=first.begin(); it!=first.end(); ++it)  
    cout << " " << *it;  
  
  cout << endl;  
  
  second.resize(first.size());  
  transform (first.begin(), first.end(), second.begin(), op_increase);                                 
												  
  cout << "\n after op_increase first contains:";  
  for (it=first.begin(); it!=first.end(); ++it)  
    cout << " " << *it;  
  
  cout << endl;  
  
  cout << "after op_increase second contains:";  
  for (it=second.begin(); it!=second.end(); ++it)  
    cout << " " << *it;  
  
  cout << endl;  
  
  transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);  
                                                 
 
  cout << "\n after op_sum first contains:";  
  for (it=first.begin(); it!=first.end(); ++it)  
    cout << " " << *it;  
  
  cout << endl;  
  
  cout << "after op_sum second contains:";  
  for (it=second.begin(); it!=second.end(); ++it)  
    cout << " " << *it;  
  
  cout << endl;
  return 0;  
}  

C++:STL标准库:算法:更易型算法_第1张图片

1.2:copy() , copy_if() 算法

copy()算法
描述:容器内指定范围的元素拷贝到另一容器中。

function template
std::Copy
Copy range of elements
   1: Copies the elements in the range [first,last) into the range beginning at result.
   2: The function returns an iterator to the end of the destination range.

The behavior of this function template is equivalent to
template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator reslut)
{
	while(first != last)
	{
		*result = *first;
		++result;
		++first;
    }
    return result;
}


举例

#include 
#include
#include

int main(){
	int myints[] = {10,20,30};
	std::vector<int> vec(3);
	std::copy(myints,myints+3,vec.begin());
}

copy_if() 算法:
描述:复制那些“符合某个给定规则”的元素

function template
std::copy_if
template <class InputIterator,class OutputIterator,class UnaryPredicate>
OutputIterator copy_if(InputIterator first, InputInterator last, 
                   OutputIterator result,UnaryPredicate pred)
{
	while(first != last)
	{
		if(pred(*first))
		{
        	*result = *first;
        	++result;
        }
        ++first;
	}
	return result;	
}

Example

// copy_if example
#include
#include
#include

int main()
{
	std::vector<int> foo = {25,15,-5,-45};
	std::vector<int> bar(foo.size());
	
	// copy only positive numbers (只复制正数)
	auto it = std::copy_if(foo.begin(),foo.end(),bar.begin(),
	    [](int i){
        	return !(i<0);
        });
    return 0;
}

你可能感兴趣的:(#,C++,标准库学习,c++,STL)