实验要求:
1.撰写自己的算法和函数,结合容器和迭代器解决序列变换(如取反、平方、立方),像素变换(二值化、灰度拉伸);
2.用set存储学生信息,并进行增删改查操作;
3.输入一个字符串,用map统计每个字符出现的次数并输出字符及对应的次数。
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 transCub(T a[],T b[],int nNum)//立方
{
for(int i=0;i<nNum;i++)
{
b[i] = a[i]*a[i]*a[i];
}
}
模板函数实现
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];
}
void transSub(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 T>
class MyThreshold{
public:
MyThreshold(int n=128):_nThreshold(n){}
int operator()(T val)
{
return val<_nThreshold?0:1;
}
int _nThreshold;
};
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 strNme,ostream& os, T begin, T end)
{
os<<strNme<<":";
for(;begin!=end;begin++)
{
os<<*begin<<"\t";
}
os<<endl;
}
2.用set存储学生信息,并进行增删改查操作
用set存储
void TestSet()
{
vector<studentInfo> students;
students.push_back(studentInfo("10021","Zhang san"));
students.push_back(studentInfo("10002","Li si"));
students.push_back(studentInfo("10003","Wang wu"));
students.push_back(studentInfo("10011","Wang Liu"));
students.push_back(studentInfo("10010","Wu Liu"));
set<studentInfo> studentSet(students.begin(),students.end());
outputCont("student set",cout,studentSet.begin(),studentSet.end());
}
用vector存储
void TestVector()
{
vector<studentInfo> students;
students.push_back(studentInfo("10021","Zhang san"));
students.push_back(studentInfo("10002","Li si"));
students.push_back(studentInfo("10003","Wang wu"));
students.push_back(studentInfo("10011","Wang Liu"));
students.push_back(studentInfo("10010","Wu Liu"));
vector<studentInfo>::iterator it = students.begin();
advance(it,2);
sort(it, students.end());
outputCont("students sorted",cout,students.begin(),students.end());
students[2]._strName = "zy";
for(vector<studentInfo>::iterator it=students.begin();
it!=students.end();it++)
{
it‐>_strName = "zy";
}
outputCont("students modified",cout,students.begin(),students.end());
}
3.输入一个字符串,用map统计每个字符出现的次数并输出字符及对应的次数。
void TestMap()
{
map<string,int> stu;
stu["01"] = 100;
stu["10"] = 99;
stu["05"] = 98;
stu["10"] = 95;
for(map<string,int>::iterator it=stu.begin();it!=stu.end();it++)
{
cout<<it‐>first<<" "<<it‐>second<<endl;
}
}