运算符的重载,函数模版,vector

1.重载

运算符成员函数
(1)对象3=对象2+对象1

RMB operator+(RMB d)
{ return RMB(yuan+d.yuan+(jf+d.jf)/100);} //人民币加的运算符重载
RMB operator(double rate)
{ return RMB((yuan+jf/100)
rate);} //乘法的重载
RMB expense2(RMB principle, double rate) //重载运算符的使用
{
  RMB interest = principle * rate; //本金乘利息
 return principle + interest; //连本带利
}

例题1:
#include<iostream>
using namespace std;
class RMB//人民币类 
{
private:
    unsigned int yuan;//元 
    unsigned int jf;//角,分
public:
    RMB(double d)
    {
        yuan=d;
        jf=(d-yuan)/100;
    } 
    RMB interest(double rate);//计算利息
    RMB add(RMB d); 
    void display()
    {
        cout<<(yuan+jf/100.0)<<endl;
    }
    RMB operator+(RMB d)
    {
        yuan+d.yuan+(jf+d.jf)/100;//人民币加的运算符重载 
    }
    RMB operator*(double rate)
    {
        return RMB((yuan+jf/100)*rate);
    }   
};
RMB RMB::interest(double rate)
{
    return RMB((yuan+jf/100)*rate);
}
RMB RMB::add(RMB d)
{
    return RMB (yuan+d.yuan+(jf+d.jf)/100);
}
RMB expensel(RMB principle,double rate)
{
    RMB interest=principle.interest(rate);
    return principle.add(interest);
}
RMB expense2(RMB principle,double rate)
{
    RMB interest=principle*rate;
    return (principle+interest);
}
int main()
{
    RMB x=10000.0;
    double yrate=0.035;
    expensel(x,yrate).display();
    expense2(x,yrate).display();    
}
运算结果:
10350
10350

(2)下面的程序将运算符+和++声明为人民币类的友元:
+由值返回,++由引用返回
  • 对于operator+(),两个对象相加, 不改变其中任一个对象。而且它必须生成一个结果对象来存放加法的结果,并将该结果对象以值的方式返回给调用者。
  • operator++()确实修改了它的参数, 而且其返回值要求是左值,这个条件决定了它不能以值返回
例题1
class RMB�    
{�     
public:�      
        RMB(unsigned int d, unsigned int c)
       {
               yuan = d;
               jf = c;
       }�      
       friend RMB operator +(const RMB&,const RMB&);�      
       friend RMB& operator ++(RMB&);�      
       void display()
       {  
                cout <<(yuan + jf / 100.0) << endl; 
       }�     
protected:�      
                unsigned int yuan;�      
                unsigned int jf;�    
};
RMB operator+(const RMB& s1,const  RMB& s2)�     
{�  
   unsigned int jf = s1.jf + s2.jf;�  
   unsigned int yuan = s1.yuan + s2.yuan;�  
   RMB result( yuan, jf );�  
   return result;�    
}
    
RMB& operator ++(RMB& s) 
{�   
  s.jf ++;�  
  if(s.jf >= 100)
       {�   
   s.jf -= 100;�   
   s.yuan++;�  
    }   
     return s;�   
}
例题2:
#include <iostream>
using namespace std;
int a1(4),b1(5),c1;
class zyz
{
    int m_z;
public:
    zyz(int z)
    {
        m_z=z;
    }
    zyz()
    {
        m_z=10;
    }
    int getz()
    {
        return m_z; 
    }
    void setz(int z)
    {
        m_z=z;
    }
    friend int operator+(zyz t1,zyz t2);//友元
};
zyz a2(11),b2(1),c2;
int operator+(zyz t1,zyz t2)
{
    return t1.getz()+t2.getz();
}
int main(int argc, char *argv[])
{
    c1=a1+b1;//operator+(a1+b1);=>operator+(int i1,int i2);
    c2=a2+b2;//c1=operator+(a2+b2);=>int operator+(int t1,int t2);
    cout<<"c1="<<c1<<endl;
    return 0;
}
运行结果:
c1=9

函数模板

template<类型形式参数表>
返回类型 FunctionName(形式参数表)
{
//函数定义体
}

例题1:
 #include<iostream>
using namespace std;
template<class X>
X Max(X a,X b)
{    return (a>b?a:b);    }
int main(int argc,char *argv[])
{   int x1=5;
    int  x2=4;
    cout<<"mxa int="<<Max<double>(x1,x2)<<endl;
    double x3=4.43;
    double x4=6.434;
    cout<<"mxa double="<<Max<double>(x3,x4)<<endl;
    char x5='s';
    char x6='R';
    cout<<"mxa char="<<Max(x5,x6)<<endl;
    return 0;
}

运算结果:
max int=5
max int=6.434
max char=s


例题2:
#include<iostream>
using namespace std;
template<class X,class Y>
class Test
{
    X m_t1;
    Y m_t2;
public:
    Test(X t1,X t2)
    {
        m_t1=t1;
        m_t2=t2;
    }
    void show()//这是写在class里面的
    {
        cout<<"T1="<<m_t1<<'\n'<<"T2="<<m_t2<<endl; 
    }
    void print();//一般情况写在class里面的,如果写在外面需要写template<class X,class Y>和void Test<X,Y>::print()

};
template<class X,class Y>
void Test<X,Y>::print()
{
    cout<<"T1="<<m_t1<<'\n'<<"T2="<<m_t2<<endl; 
}
int main()
{
    Test<int,char>t(10,'s');
    t.show();
    t.print();
}

运算结果:
T1=10
T2=s
T1=10
T2=s

vector

1.初始化 vector 容器方法

(1)vector<elementType> v; // 创建一个没有任何元素的空容器
(2)vector<elementType> v(otherVec); //调用拷贝构造函数创建新容器
(3)vector<elementType> v(size); //创建一个大小为size的对象v,并使用默认构造函数初始化该向量
(4)vector<elementType> v(n,elem); //创建一个大小为n的容器,并使用元素elem初始化每一个元素
(5)vector<elementType> v(begin,end); //创建容器v,并使用(begin,end)之间的元素初始化容器

2.元素的插入

(1)veclist.push_back(elem); //将elem的一个拷贝插入到veclist的末尾
(2)veclist.insert(position,elem); //将elem的一个拷贝插入到指定的position的位置上
(3)veclist.insert(position,n,elem); //将elem的n个拷贝插入到由position指定的位置上
(4)veclist.insert(position,beg,end); //将从迭代器 beg至end-1 之间的元素插入到veclist 的position位置上

例题1:
#include<iostream>
#include<vector> 
using namespace std;
vector<int> v;
int main()
{
    for(int i=0;i<11;i++)
    {
        v.push_back(i);
    }
    for(int j=0;j<v.size();j++)
    {
        cout<<v[j]<<" ";
    }   
    cout<<endl;
}
运算结果:
 0 1 2 3 4 5 6 7 8  9 10 


例题2:
 #include <vector>
 #include<iostream>
using namespace std;
vector<int> num;// STL中的vector容器
int main()
{
    int element;// 从标准输入设备读入整数,直到输入的是非整型数据为止
    while (cin >> element)
    num.push_back(element);//访问容器内的元素
    for(int i=0; i<num.size(); i++)
    {
       cout<<num[i]<<'\t';
       
    }
    cout<<endl;
    for(vector<int>::iterator it=num.begin();it<num.end();it++)// 使用iterator访问
    {
        cout<<*it<<"\t";

        
    }
    cout<<endl;
    for(vector<int>::reverse_iterator it=num.rbegin();it<num.rend();it++)//倒着输出
    {
        cout<<*it<<"\t";
        
    }
    cout<<endl;
}
在键盘上输入3 4 5  3.55
运算结果:
3 4 5 3
3 4 5 3
3 5 4 3

迭代器的使用方法(元素的删除)

(1)veclist.clear(); //清空容器中所有元素
(2)veclist.erase(position); //删除position指定位置的元素
(3)veclist.erase(beg,end); //删除从beg至end-1之间的元素
(4)veclist.pop_back(); //删除最后一个元素

(1)vector<int>::iterator iter=num.begin();
(2)num.pop_back(); //删除最后一个元素
(3)num.erase(iter); //删除容器的第一个元素
(4)num.erase(iter, iter+2); //删除前2个元素

 #include <vector>
 #include<iostream>
using namespace std;
void show (vector<int> vi)
{
    vector<int>::iterator it;
    it=vi.begin();
    while(it!=vi.end())
    cout<<*it++<<' ';
    cout<<endl;
}
int main()
{
    vector<int> vi(3,90);//表示容器里有3个90:90 90 90 
    show(vi);
    int a[5]={3,4,5,6,7};
    vi.insert(vi.begin(),a,a+5);//从第一个位置插入3,4,5,6,7 
    show(vi);
    vi.push_back(100);//从尾部插入数字100 
    show(vi);
    cout<<"size:"<<vi.size()<<endl;
    vi.assign(5,99);//重新设置容器,容器里有5个99 
    show(vi);
    cout<<"size:"<<vi.size()<<endl; 
}
运算结果:
90 90 90
3 4 5 6 7 90 90 90
3 4 5 6 7 90 90 90 100
size=9
99 99 99 99 99
size=5

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

推荐阅读更多精彩内容

  • 模板 模板是C++语言相对较新的一个重要特性。模板使程序员能够快速建立具有类型安全的类库集合和函数集合,它的实现,...
    帅碧阅读 934评论 2 4
  • C++基础 模板及标准模板库 模板的作用模板使程序员能够快速的建立具有类型安全得库集合和函数集合,它的实现,方便了...
    I踏雪寻梅阅读 509评论 0 4
  • 前言: 详细介绍: List:元素有放入顺序,元素可重复Map:元素按键值对存储,无放入顺序Set:元素无放入顺序...
    YBshone阅读 8,607评论 0 17
  • 上次更新简书是在20天前了,这两周实在是太忙了。今天要分享的幻灯片是可口可乐的简介。(一点要点击查看大图!!!) ...
    黄显浩阅读 903评论 3 10
  • 受到蔡康永老师的启发,突然间悟到了,说话不仅仅只有技巧,还有道。说什么话就代表你是什么的人。一个人的语言反映了他的...
    RocheLimit007阅读 388评论 0 0