运算符的重载,函数模版,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
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)<
(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 
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="<

函数模板

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

例题1:
 #include
using namespace std;
template
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="<(x1,x2)<
 #include
using namespace std;
vector num;// STL中的vector容器
int main()
{
    int element;// 从标准输入设备读入整数,直到输入的是非整型数据为止
    while (cin >> element)
    num.push_back(element);//访问容器内的元素
    for(int i=0; i::iterator it=num.begin();it::reverse_iterator it=num.rbegin();it

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

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

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

 #include 
 #include
using namespace std;
void show (vector vi)
{
    vector::iterator it;
    it=vi.begin();
    while(it!=vi.end())
    cout<<*it++<<' ';
    cout< 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:"<

你可能感兴趣的:(运算符的重载,函数模版,vector)