c++ STL(标准模板库)应用


一、实验内容

1.撰写自己的算法和函数,结合容器和迭代器解决序列变换(如取反、平方、立方),像素变换(二值化、灰度拉伸);

2.用set存储学生信息,并进行增删改查操作;

3.输入一个字符串,用map统计每个字符出现的次数并输出字符及对应的次数。

二、相关过程

在这里插入图片描述

1.撰写算法解决序列变换

1.1 非模板函数实现

void transInv(int a[],int b[],int nNum)      //对元素取反
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = -a[i];
    }
}
void transSqr(int a[],int b[],int nNum)      //对元素求平方
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = a[i]*a[i];
    }
}
void transPow(int a[],int b[],int nNum)      //对元素求立方
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = a[i]*a[i]*a[i];
    }
}
 
void TestTrans()
{
   
    const int N=5;
    int a[N] = {
   1,2,3,4,5};
    outputCont("a",cout,a,a+N);
    int b[N];
    transInv(a,b,N);
    outputCont("Inv a",cout,b,b+N);
    transSqr(a,b,N);
    outputCont("Sqr a",cout,b,b+N);
    transPow(a,b,N);
    outputCont("Pow a",cout,b,b+N);
    transInvT(a,b,N);
}

运行结果:
在这里插入图片描述

1.2 模板函数实现

template <typename T>
void transInvT(T a[],T b[],int nNum)      //对元素取反
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = -a[i];
    }
}
template <typename T>
void transSqrT(T a[],T b[],int nNum)      //对元素求平方
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = a[i]*a[i];
    }
}
template <typename T>
void transPow(T a[],T b[],int nNum)      //对元素求立方
{
   
    for(int i=0;i<nNum;i++)
    {
   
        b[i] = a[i]*a[i]*a[i];
    }
}
template<typename T>
T InvT(T a)
{
   
    return -a;
}
 
 
template <typename inputIter,typename outputIter,typename MyOperator>
void transInvT(inputIter begInput,inputIter endInput,outputIter begOutput,MyOperator op)      //对元素取反
{
   
    for(;begInput!=endInput;begInput++,begOutput++)
    {
   
         //*begOutput = -(*begInput);
         *(begOutput) = op(*begInput);
    }
}
 
template <typename T>
void outputCont(string strName,ostream& os,T begin,T end)  //输出元素
{
   
    os<<strName<<":";
    for(;begin!=end;begin++)
    {
   
        os<<*begin<<"\t";
    }
    os<<endl;
}
template<typename T>
class MyThreshold
{
   
public:
    int _nThreshold;
    MyThreshold(int n=128)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值