泛型设计的思路:
泛型设计的产生是为了解决 把相同的类或者模板类之间有相似的方法 函数 抽象出来
就是对算法的抽象 迭代器的出现 是为了让抽象后的算法能能够访问不同类的内部数据
而出来的概念函数对象可以理解为 对某一算法的进一步限制提出要求
1.容器
(1)vector 有点类似 动态数组
#include<iostream>
#include<vector>//vector
#include<iterator>//迭代器
#include<algorithm>//算法
#include<functional>//函数对象
using namespace std;
void main()
{
int a[] = {1,5,3,4,2};
vector<int> v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<<v[i]<<endl; //普通的遍历 这里用到了 []
vector<int>::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
cout<<v.size()<<endl;//vector的size大小
cout<<v.at(2)<<endl;//at 指定下标返回对应元素值 at的参数是下标 这个是支持随机访问
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<<v.empty()<<endl;//判断vector是否为空
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
}
注意头文件的使用
(2)stack
#include<iostream>
#include<stack>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
//栈先进后出
void main()
{
int a;
stack<int> s;
for(int i = 0;i<5;i++)
{
cin>>a;
s.push(a);//压栈
}
cout<<s.top()<<endl;
//for( i = 0;i<5;i++)
// cout<<s.pop()<<endl; //s.pop()只能弹出 没有返回 pop的值
for( i = 0;i<5;i++)
s.pop();//出栈
cout<<s.empty()<<endl;//判断是否为空
}
(3).queue
#include<iostream>
#include<queue>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
//队列先进先出
void main()
{
int a;
queue<int> q;
for(int i = 0;i < 5;i++)
{
cin>>a;
q.push(a);
}
cout<<q.front()<<endl;//输出队列头部元素
cout<<q.back()<<endl;//输出队列尾部元素
for( i = 0;i < 5;i++)
{
q.pop();
}
cout<<q.empty()<<endl;
}
(4)string
#include<iostream>
#include<string>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
void main()
{
string s1 ="hello",s2("nihao");
cout<<s1.empty()<<endl;
cout<<s2.size()<<endl;
for(int i = 0;s1[i]!=NULL;i++)
cout<<s1[i];
cout<<endl;
cout<<s2.at(3)<<endl;
string s3 = s1 + s2;
cout<<s3<<endl;
s3 += s1;
cout<<s3<<endl;
cout<<s1.substr(1,3)<<endl;
cout<<(s1 == s2)<<endl;
cout<<(s2 != s1)<<endl;
cout<<(s3 > s1)<<endl;
cout<<(s3 < s1)<<endl;
cout<<(s1 >= s2)<<endl;// hello >= nihao 返回0
cout<<(s1 <= s2)<<endl;//hello <= nihao 返回1
}
2.迭代器
vector<int>::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
3.算法
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
void main()
{
int a[] = {1,5,3,4,2};
vector<int> v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<<v[i]<<endl; //普通的遍历 这里用到了 []
vector<int>::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
cout<<v.size()<<endl;//vector的size大小
cout<<v.at(2)<<endl;//at 指定下标返回对应元素值 at的参数是下标 这个是支持随机访问
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<<v.empty()<<endl;//判断vector是否为空
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
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<<s1<<endl;
find方法
int *pos = find(v.begin(),v.end(),3);
if(pos == v.end())
cout<<"Not Found"<<endl;//find 这个算法是 找到元素则返回这个元素 没找到返回最后一个元素的下一个
else
cout<<"index = "<<pos - v.begin()<<endl;
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<<endl;
cout<<b<<endl;
b = count_if(v.begin(),v.end(),isodd);
cout<<b<<endl;
4.函数对象
(1)普通函数
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
int isbigger(int a,int b)
{
return (a>b);
}
void main()
{
int a[] = {1,5,3,4,2};
vector<int> v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<<v[i]<<endl; //普通的遍历 这里用到了 []
vector<int>::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
cout<<v.size()<<endl;//vector的size大小
cout<<v.at(2)<<endl;//at 指定下标返回对应元素值 at的参数是下标 这个是支持随机访问
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<<v.empty()<<endl;//判断vector是否为空
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
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<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
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<int> v(a,a+5);//vector的构造
// for(int i = 0;i<5;i++)
// cout<<v[i]<<endl; //普通的遍历 这里用到了 []
vector<int>::iterator itr;//声明vector迭代器
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
cout<<v.size()<<endl;//vector的size大小
cout<<v.at(2)<<endl;//at 指定下标返回对应元素值 at的参数是下标 这个是支持随机访问
//访问分为顺序访问和随机访问 不是所有的容器都有随机访问
cout<<v.empty()<<endl;//判断vector是否为空
v.push_back(100);//在vector尾部插入100
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";//通过迭代器遍历
cout<<endl;
sort(v.begin(),v.end(),isbigger);
for(itr = v.begin();itr < v.end();itr++)
cout<<*itr<<" ";
cout<<endl;
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<iostream>
#include<iomanip>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
#include<cmath>
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 <c1.imag;
}
ostream operator<<(ostream& out, const Complex& c)
{
out <<c.real<<" + "<<c.imag<<"i ";
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<Complex> a(n);
vector<Complex>::iterator itr;
for(itr = a.begin();itr<a.end();itr++)
cin>>*itr;
sort(a.begin(),a.end(),isgreater);
for(itr = a.begin();itr<a.end();itr++)
{
cout<<*itr<<endl;
}
}