美文网首页
运算符的重载,函数模版,vector

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

作者: 萌面大叔2 | 来源:发表于2017-02-15 13:09 被阅读0次

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

相关文章

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

    1.重载 运算符成员函数 (1)对象3=对象2+对象1 RMB operator+(RMB d){ return ...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • C++面向对象-运算符重载

    运算符重载 运算符重载又称为操作符重载,可以为运算符增加一些新的功能,全局函数和成员函数都支持运算符重载,我们通过...

  • 日记之旅第七天

    上午:重点讲解了函数重载,其中包括运算符重载,函数成员重载。运算符重载结合了昨天所讲的友元函数一起使用 下午:复习...

  • c++ 运算符重载

    重载方法 运算符的重载实质上是函数的重载。函数的重载就是对一个已有函数赋予新的含义,使之实现新的功能。 重载运算符...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • 运算符重载

    运算符重载 运算符重载的本质是函数重载。 语法格式 operator 运算符名称 在一起构成了新的函数名。比如对于...

  • 4.0 C++远征:重载运算符

    重载运算符 [TOC] 四、重载运算符 ​ 概念 : 给原有运算符赋予新功能。 ​ 本质 : 函数重载。 ...

  • find_if 仿函数

    一个类或者结构重载小括号运算符 使其看起来像一个函数注意 vector里面存的是非指针 重载的小括号的参数也要是非...

  • 1.2.19_C++ 函数调用运算符 () 重载

    C++ 重载运算符和重载函数 函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的...

网友评论

      本文标题:运算符的重载,函数模版,vector

      本文链接:https://www.haomeiwen.com/subject/ynhhpttx.html