泛型设计的思路:
泛型设计的产生是为了解决 把相同的类或者模板类之间有相似的方法 函数 抽象出来
就是对算法的抽象 迭代器的出现 是为了让抽象后的算法能能够访问不同类的内部数据
而出来的概念
函数对象可以理解为 对某一算法的进一步限制提出要求
1.容器
(1)vector 有点类似 动态数组
#include
#include//vector
#include//迭代器
#include//算法
#include//函数对象
using namespace std;
void main()
{
int a[] = {1,5,3,4,2};
vector v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<
vector::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
cout<
cout<
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
}
注意头文件的使用
(2)stack
#include
#include
#include
#include
#include
using namespace std;
//栈先进后出
void main()
{
int a;
stack s;
for(int i = 0;i<5;i++)
{
cin>>a;
s.push(a);//压栈
}
cout<
//for( i = 0;i<5;i++)
// cout<
for( i = 0;i<5;i++)
s.pop();//出栈
cout<
}
(3).queue
#include
#include
#include
#include
#include
using namespace std;
//队列先进先出
void main()
{
int a;
queue q;
for(int i = 0;i < 5;i++)
{
cin>>a;
q.push(a);
}
cout<
cout<
for( i = 0;i < 5;i++)
{
q.pop();
}
cout<
}
(4)string
#include
#include
#include
#include
#include
using namespace std;
void main()
{
string s1 ="hello",s2("nihao");
cout<
cout<
for(int i = 0;s1[i]!=NULL;i++)
cout<
cout<
cout<
string s3 = s1 + s2;
cout<
s3 += s1;
cout<
cout<
cout<<(s1 == s2)<
cout<<(s2 != s1)<
cout<<(s3 > s1)<
cout<<(s3 < s1)<
cout<<(s1 >= s2)<= nihao 返回0
cout<<(s1 <= s2)<
}
2.迭代器
vector::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
3.算法
#include
#include
#include
#include
#include
using namespace std;
void main()
{
int a[] = {1,5,3,4,2};
vector v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<
vector::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
cout<
cout<
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
sort(v.begin(),v.end());
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
}
对 vector 可以 sort()
sort(v.begin(),v.end());
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//这个默认是从小到大排 要它从大到小排要加函数对象
对string 也可以sort()
sort(s1.begin(),s1.end());
cout<
find方法
int *pos = find(v.begin(),v.end(),3);
if(pos == v.end())
cout<<"Not Found"<
else
cout<<"index = "<
find_if方法
int isodd(int x)
{
return x%2;
}
cout<<*(find_if(v.begin(),v.end(),isodd));//按照isodd的规则查找 找到第一个满足的元素就返回了
count 和 count_if
int b = count(v.begin(),v.end(),3);
cout<
cout<
b = count_if(v.begin(),v.end(),isodd);
cout<
4.函数对象
(1)普通函数
#include
#include
#include
#include
#include
using namespace std;
int isbigger(int a,int b)
{
return (a>b);
}
void main()
{
int a[] = {1,5,3,4,2};
vector v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<
vector::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
cout<
cout<
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
sort(v.begin(),v.end(),isbigger);
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
}
函数对象
int isbigger(int a,int b)
{
return (a>b);
}
调用方法
sort(v.begin(),v.end(),isbigger);
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
(2)类
#include
#include
#include
#include
#include
using namespace std;
int isbigger(int a,int b)
{
return (a>b);
}
class Big
{
public:
int operator()(int a,int b)
{
return (a>b);
}
};
void main()
{
int a[] = {1,5,3,4,2};
vector v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<
vector::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
cout<
cout<
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<
sort(v.begin(),v.end(),isbigger);
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
cout<
sort(v.begin(),v.end(),Big());
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
}
函数对象
class Big
{
public:
int operator()(int a,int b)
{
return (a>b);
}
};
调用方法
sort(v.begin(),v.end(),Big());
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
最后用一个复数类来解决
注意 函数对象要能访问到类的数据要用到 友元函数 或者友元类
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Complex;
ostream operator<<(ostream& out,const Complex& c);
istream& operator>>(istream& in,Complex& c);
Complex Add(const int a,const Complex &c1);
Complex Jian(const int a,const Complex &c1);
Complex Chen(const int a,const Complex &c1);
Complex Chu(const int a,const Complex &c1);
int isgreater(Complex x,Complex y);
class Complex
{
friend Complex Add(const int a,const Complex &c1);
friend Complex Jian(const int a,const Complex &c1);
friend Complex Chen(const int a,const Complex &c1);
friend Complex Chu(const int a,const Complex &c1);
public:
Complex(double r=0, double i=0) :real(r), imag(i){}
~Complex(){}
Complex operator+(const Complex& c1)const;
Complex operator-(const Complex& c1)const;
Complex operator*(const Complex& c1)const;
Complex operator/(const Complex& c1)const;
Complex operator-();
Complex operator++();
Complex operator++(int);
bool operator==(const Complex& c1)const;
bool operator!=(const Complex& c1)const;
bool operator>(const Complex& c1)const;
bool operator<(const Complex& c1)const;
friend ostream operator<<(ostream& out, const Complex& c);
friend istream& operator>>(istream& in,Complex& c);
friend int isgreater(Complex x,Complex y);
private:
double real;
double imag;
};
Complex Complex::operator+(const Complex& c1)const
{
Complex temp;
temp.real = real + c1.real;
temp.imag = imag + c1.imag;
return temp;
}
Complex Complex::operator-(const Complex& c1)const
{
Complex temp;
temp.real = real - c1.real;
temp.imag = imag - c1.imag;
return temp;
}
Complex Complex:: operator*(const Complex& c1)const{
Complex temp;
temp.real = real * c1.real;
temp.imag = imag * c1.imag;
return temp;
}
Complex Complex:: operator/(const Complex& c1)const
{
Complex temp;
temp.real = real / c1.real;
temp.imag = imag / c1.imag;
return temp;
}
Complex Add(const int a,const Complex &c1)
{
Complex temp;
temp.real = a + c1.real;
temp.imag = c1.imag;
return temp;
}
Complex Jian(const int a,const Complex &c1)
{
Complex temp;
temp.real = c1.real - a;
temp.imag = c1.imag;
return temp;
}
Complex Chen(const int a,const Complex &c1)
{
Complex temp;
temp.real = c1.real * a;
temp.imag = c1.imag;
return temp;
}
Complex Chu(const int a,const Complex &c1)
{
Complex temp;
temp.real = c1.real / a;
temp.imag = c1.imag;
return temp;
}
Complex Complex::operator-()
{
return Complex(-real, -imag);
}
Complex Complex::operator++()
{
++real;
++imag;
return Complex(real, imag);
}
Complex Complex::operator++(int)
{
Complex temp(*this);
real++;
imag++;
return temp;
}
bool Complex::operator==(const Complex& c1)const
{
return real == c1.real&&imag == c1.imag;
}
bool Complex::operator!=(const Complex& c1)const
{
return real != c1.real && imag != c1.imag;
}
bool Complex::operator>(const Complex& c1)const
{
return real > c1.real && imag > c1.imag;
}
bool Complex:: operator<(const Complex& c1)const
{
return real < c1.real && imag
}
ostream operator<<(ostream& out, const Complex& c)
{
out <
return out;
}
istream& operator>>(istream& in,Complex& c)
{
in>>c.real>>c.imag;
return in;
}
int isgreater(Complex x,Complex y)
{
return sqrt(x.real*x.real+x.imag*x.imag)>sqrt(y.real*y.real+y.imag*y.imag);
}
void main()
{
int n;
cout<<"请输入复数个数";
cin>>n;
vector a(n);
vector::iterator itr;
for(itr = a.begin();itr
cin>>*itr;
sort(a.begin(),a.end(),isgreater);
for(itr = a.begin();itr
{
cout<<*itr<
}
}