您的位置:首页 > 编程语言 > C语言/C++

c++ “算法学习”

2016-11-30 21:23 369 查看
#include <iostream>
using namespace std;
#include <algorithm>
#include <functional>
#include <set>
#include <vector>
#include <string>

//遍历vector对象
void printV(vector<int> v1)
{
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
}

//函数对象 重载了()的类的对象
template <typename T>
class ShowElemt
{
public:
ShowElemt()
{
this->n = 0;
}
void operator()(T& t)
{
cout << t << " ";
n++;
}
void printN()
{
cout << "count=" << n << endl;
}
private:
int n;
};
void main1()
{
int a = 10;
ShowElemt<int> show1;
show1(a);
}
//函数对象,可以保持某些状态
void main2()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
ShowElemt<int> show1;
/*
template<class _InIt,class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{   // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);

return (_STD move(_Func));
}
*/
show1=for_each(v1.begin(), v1.end(), show1);
show1.printN();
}

//一元谓词
template <typename T>
class IsDiv
{
public:
IsDiv(const T& divisor)
{
this->divisor = divisor;
}
bool operator()(T& t)
{
return(t%divisor == 0);
}
private:
T divisor;
};
void main3()
{
IsDiv<int> IsDiv1(4);
vector<int> v2;
for (int k = 10; k < 33; k++)
{
v2.push_back(k);
}
/*
template<class _InIt,
class _Pr> inline
_InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{   // find first satisfying _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
return (_Rechecked(_First,
_Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
*/
vector<int>::iterator it=find_if(v2.begin(), v2.end(), IsDiv1);
if (it != v2.end())
{
cout << "第一个被4整除的元素" << *it << endl;
}
else
{
cout << "数组中未找到被4正常的元素" << endl;
}
}

//二元函数对象
template <typename T>
class SumAdd
{
public:
T operator()(T& a, T& b)
{
T c;
c = a + b;
return c;
}
};
void main4()
{
vector<int> v1, v2, v3;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);

v2.push_back(2);
v2.push_back(4);
v2.push_back(6);

v3.resize(10);
/*
template<class _InIt1,class _InIt2,class _OutIt,class _Fn2> inline
_OutIt _Transform(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{   // transform [_First1, _Last1) and [_First2, ...) with _Func
for (; _First1 != _Last1; ++_First1, ++_First2, ++_Dest)
*_Dest = _Func(*_First1, *_First2);
return (_Dest);
}
*/
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), SumAdd<int>());
printV(v3);
}
//二元谓词
template<typename T>
class MyComper
{
public:
bool operator()(T a, T b)
{
return a < b;
}
};
void main5()
{
vector<int> v1(10);
for (int i = 0; i < 10; i++)
{
int tmp = rand() % 100;
v1[i] = tmp;
}
printV(v1);
/*
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{   // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);

return (_STD move(_Func));
}
*/
for_each(v1.begin(), v1.end(), ShowElemt<int>());
cout << endl;

/*
template<class _RanIt,
class _Pr> inline
void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
{   // order [_First, _Last), using _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
_Sort(_Unchecked(_First), _Unchecked(_Last), _Last - _First, _Pred);
}
*/
sort(v1.begin(), v1.
4000
end(), MyComper<int>());
printV(v1);
}

//set不区分大小写查找元素 二元谓词的应用
struct CompareNoCase
{
bool operator()(const string &str1, const string &str2)
{
string str1_;
str1_.resize(str1.size());
transform(str1.begin(), str1.end(), str1_.begin(), tolower);

string str2_;
str2_.resize(str2.size());
transform(str2.begin(), str2.end(), str2_.begin(), tolower);
return (str1_ < str2_);
}
};
void main6()
{
set<string> set1;
set1.insert("aaa");
set1.insert("bbb");
set1.insert("ccc");
set<string>::iterator it = set1.find("aaa");//fiad()区分大小写
if (it != set1.end())
{
cout << "find it" << endl;
}
else
{
cout << "not find it" << endl;
}
set<string, CompareNoCase> set2;
set2.insert("aaa");
set2.insert("bbb");
set2.insert("ccc");
set<string,CompareNoCase>::iterator it1 = set2.find("aAa");//fiad()区分大小写
if (it1 != set2.end())
{
cout << "find it" << endl;
}
else
{
cout << "not find it" << endl;
}
}

//预定义函数对象
void main7()
{
int a = 4;
int b = 5;
/*
template<class _Ty = void>
struct plus
: public binary_function<_Ty, _Ty, _Ty>
{   // functor for operator+
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{   // apply operator+ to operands
return (_Left + _Right);
}
};
*/
int c = plus<int>()(a, b);//预定函数对象,能实现不同数据类型的加法运算
cout << "c=" << c << endl;
string str1 = "zww";
string str2 = "wkg";
string str3 = plus<string>()(str1, str2);
cout << "str3:" << str3 << endl;

vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("aaa");
v1.push_back("aaa");
v1.push_back("zzz");
string s2 = "aaa";
/*
template<class _Ty = void>
struct greater
: public binary_function<_Ty, _Ty, bool>
{   // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{   // apply operator> to operands
return (_Left > _Right);
}
};
*/
sort(v1.begin(), v1.end(), greater<string>() );
for (vector<string>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << endl;
}
/*
template<class _Ty = void>
struct equal_to
: public binary_function<_Ty, _Ty, bool>
{   // functor for operator==
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{   // apply operator== to operands
return (_Left == _Right);
}
};
*/
int num=count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), s2));
cout << "num=" << num << endl;
}
//函数适配器
class IsGreat
{
public:
IsGreat(const int a)
{
tag = a;
}
bool operator()(const int& b)
{
if (b>tag)
{
return true;
}
else
{
return false;
}
}
private:
int tag;
};
void main8()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i + 1);
}
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
int num1 = count(v1.begin(), v1.end(), 3);
cout << "num1=" << num1<<endl;
//1.通过谓词求大于2的个数
int num2 = count_if(v1.begin(), v1.end(), IsGreat(2));
cout << "num2=" << num2 << endl;
//2.通过预定义函数对象
/*
template<class _Ty = void>
struct greater
: public binary_function<_Ty, _Ty, bool>
{   // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{   // apply operator> to operands
return (_Left > _Right);
}
};
*/
int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2));
cout << "num3=" << num3 << endl;
//求数组偶是个数-->自己编写二元谓词利用bind2nd函数适配器
/*
template<class _Ty = void>
struct modulus
: public binary_function<_Ty, _Ty, _Ty>
{   // functor for operator%
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{   // apply operator% to operands
return (_Left % _Right);
}
};
*/
int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus<int>(), 2));
cout << "num4=" << num4 << endl;

}

void main()
{

//main1();
//main2();
//main3();
//main4();
//main5();
//main6();
//main7();
main8();
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: