STL_算法_transform

本文详细记录了C++ Primer学习过程中关于容器操作的实践,包括transform()函数的应用及预定义函数对象的使用,通过具体示例展示了如何对容器进行修改、整合以及输出操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)


所有容器适用


transform(b1,e1,b2,op)      //把一个区间[b1,e1)内的数据经过(op)转化,放入第二个容器内

                            //也就是复制+修改(变换)    当然b2可以等于b1
transform(b1,e1,b2,b3,op)   //把两个集合里的数据整合(op)到第三个集合,当然b3=b2=b1也可以

注意:
    1、如果目标与源相同,transform()就和for_each()一样
    2、如果想以某值替换符合规则的元素,应使用replace()算法。


/**------http://blog.youkuaiyun.com/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<iterator>
#include<algorithm>
using namespace std;
/*****************************************
//所有容器适用
transform(b1,e1,b2,op)      //把一个区间[b1,e1)内的数据经过(op)转化,放入第二个容器内
                            //也就是复制+修改(变换)    当然b2可以等于b1
transform(b1,e1,b2,b3,op)   //把两个集合里的数据整合(op)到第三个集合,当然b3=b2=b1也可以

注意:
    1、如果目标与源相同,transform()就和for_each()一样
    2、如果想以某值替换符合规则的元素,应使用replace()算法。
*****************************************/
/**----------------------------------------------------------------------------------
预定义的函数对象
negate<type>()       *(-1)              equal_to<type>()         ==
plus<type>()         +                  not_equal_to<type>()     !=
minus<type>()        -                  less<type>()              <
multiplies<type>()   *                  greater<type>()          >
divides<type>()      /                  less_equal<type>()        <=
modulus<type>()      %                  greater_equal<type>()    >=
                                        logical_not<type>()       !
                                        logical_and<type>()      &&
                                        logical_or<type>()       ||
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::transform                     所有排序容器适用                          algorithm
--------------------------------------------------------------------------------------
template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op );

template < class InputIterator1, class InputIterator2,
           class OutputIterator, class BinaryOperator >
  OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryOperator binary_op );

//eg:
template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op )
{
  while (first1 != last1)
    *result++ = op(*first1++);  // or: *result++=binary_op(*first1++,*first2++);
  return result;
}
*************************************************************************************/

template<typename T>
void Print(const T& V)
{
    typename T::const_iterator iter = V.begin();
    while(iter != V.end())
        cout<<*iter++<<" ";
    cout<<endl;
}

int main()
{
    vector<int> ivec;
    list<int> ilist;

    for(int i=1;i<=9;i++)
        ivec.push_back(i);
    ivec.push_back(-10);
    vector<int>::iterator iv=ivec.begin();
    while(iv != ivec.end()) cout<<*iv++<<" ";
    cout<<endl;

    //利用预定于函数对本身修改----------------------符号取反
    transform(ivec.begin(),ivec.end(),ivec.begin(),negate<int>());
    Print(ivec);

    //存入list并修改原数据(*10) -----利用bind2nd(muliplies<int>(),10)
    transform(ivec.begin(),ivec.end(),back_inserter(ilist),
              bind2nd(multiplies<int>(),10));
    Print(ilist);
    //利用transform和输出流 符号取反输出
    transform(ilist.begin(),ilist.end(),ostream_iterator<int>(cout," "),negate<int>());
	cout<<endl;
/**------------------------------------------------------------------------------------**/
    //transform(b1,e1,b2,b3,op)
    //三个容器间的~
    //1、三个容器都是自己本身
    transform(ilist.begin(),ilist.end(),ilist.begin(),ilist.begin(),multiplies<int>());
    Print(ilist);
    //2、用两个容器,结果放入另一个容器||输出
    transform(ivec.begin(),ivec.end(),ilist.rbegin(),ostream_iterator<int>(cout," "),plus<int>());
    cout<<endl;
    transform(ivec.begin(),ivec.end(),ilist.rbegin(),ivec.begin(),plus<int>());
    Print(ivec);

    return 0;
}
/*******
Output:
    1 2 3 4 5 6 7 8 9 -10
    -1 -2 -3 -4 -5 -6 -7 -8 -9 10
    -10 -20 -30 -40 -50 -60 -70 -80 -90 100
    10 20 30 40 50 60 70 80 90 -100
    100 400 900 1600 2500 3600 4900 6400 8100 10000
    9999 8098 6397 4896 3595 2494 1593 892 391 110
    9999 8098 6397 4896 3595 2494 1593 892 391 110

**/


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值