c++学习笔记之 STL 泛型设计

泛型设计的思路:

泛型设计的产生是为了解决 把相同的类或者模板类之间有相似的方法 函数 抽象出来

就是对算法的抽象   迭代器的出现 是为了让抽象后的算法能能够访问不同类的内部数据

而出来的概念

函数对象可以理解为 对某一算法的进一步限制提出要求

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<

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容