scollins

1.
/****************

  功能:定义名为swap的函数模板用于交换两个变量中的数据
  参数:
    
  返回值:
         NULL.
  备注:
 
*****************/
void swap(int& a, int& b)
{    
      
       //1、不用中间变量
       //(1)用异或运算

       a ^= b;  
       b ^= a;  
       a ^= b;  

       //(2)用加减运算
/*
       a = a + b;
       b = a - b;
       a = a - b;
*/
}

2.
template<class T>inline const T& max(const T& a, const T& b){ return a > b ? a : b; }
#define max(a,b) ((a) > (b) ? (a) : (b))
define成为"宏",在C语言编程中非常重要,它在程序编译时只是在预处理的过程中实施简单的替换操作而已。由于在替换过程中可能出现的各种不安全性问题,在C++提倡采用const或者inline(内联函数)的方式替代宏。
内联函数和普通函数相比可以加快程序的运行速度,但它是以增加程序存储空间维代价的,由于不需要中断调用,在编译内联函数的时候内联函数可以直接被嵌入目标代码中。
对于短小的代码,inline可以带来一定效率的提升,且与C时代的define(宏)相比,它更安全可靠。
宏和内联函数的主要区别如下:
1.宏是代码处不加任何验证的简单替代,而内联函数是将代码直接插入调用处,而减少了普通函数调用时的资源消耗。
2.宏不是函数,只是在编译前预处理阶段将程序中有关字符串替换成宏体。
3.inline是函数,但在编译中不单独产生代码,而是将有关代码嵌入到调用处。
宏会为程序设计带来不小的安全隐患,许多由宏产生的问题就是实证。
所以在C++中引入的const关键字以及inline函数可以完成与宏同样的功能,且更安全,更可靠

3.
/***********************

  功能:遍历标准STL容器,删除其中等于指定值的元素
.
  参数:
       T1:标准STL容器,
       T2:标准STL容器中元素类型.
    
  返回值:
         NULL
  备注:
 
 ************************/

template<class T1, class T2>
void DeleteItem(T1 _Container, T2 _DeleteItemValue)
{
for(T1<T2>::iterator it(_Container.begin()); it != _Container.end();++it)
{
   T2 value=*it;
   if(_DeleteItemValue==value)
     {
       _Container.erase(it);    //关键操作
     }
  
}
}


4.
class A
{
      A(void){}
      virtual void Foo(void){}
};

int main()
{
     A a;
     char *p1 = reinterpret_cast<char*>(&a);
     char *p2 = reinterpret_cast<char*>(&a.n);
     if(p1 == p2)
     {
        cout<<"vPtr is in the end of class instance!"<<endl;
     }else
     {
        cout<<"vPtr is in the head of class instance!"<<endl;
     }
     return 1;
}

5.
#include<iostream>
#include<string>
using namespace std;

/***********************

  功能:查找字符串中没有出现过的字母(不区分大小写)
.
  参数:
       s:输入的string变量.
    
  返回值:
         string.
  备注:
 
 ************************/
string FindNonChar(string s)
{
    string tep = "abcdefghijklmnopqrstuvwxyz";
    int Instr[26]; //初始化26个字母的都不存在
    for(int i=0;i<26;i++)
       Instr[i]=0;
    for(int j=0;j<s.length();j++)
    {
       if(s[j]!=' ')
       {
       char ctemp = tolower(s[j]); //把字符串变成小写

       int num = (int)ctemp-97;   //减去97得到该字符在26个字母中的位置

       Instr[num]=1;    //如果这个字母在字符串中,就标记为1
       }
    }
    string temp="";

    for(int k=0;k<26;k++) //把不存在的字母存放在字符串temp中返回
    {
       if(Instr[k]==0)
           temp+=tep[k];   //把相应位置的字母存放到temp
    }
    return temp;
}

6.
/****************

  功能:字符串反转.
  参数:
      ch:输入的字符串
  返回值:
         NULL.
  备注:
 
 *****************/
void reverse(char *ch) 
{
   int len;
   int i;
   len = strlen(ch)-1;
   char ctemp;

   for(i = 0; i < len-i; i++)
   {
         ch[i] = ch[i] ^ ch[len-i];
         ch[len-i] = ch[i] ^ ch[len-i];
         ch[i] = ch[i] ^ ch[len-i];
   }
   ch[len+1] = 0;                   
}


7.
#include<iostream>

using namespace std;

#define defalut_size 512

/********************

  类说明:
      
      实现字符串的操作。    
  备注:
 
 *****************/
class KString{

     friend ostream& operator<< (ostream&,KString&);

public:

     KKString(const char* str=NULL);                 //赋值构造兼默认构造函数(char)

     KString(const KString &other);                  //赋值构造函数(KString)

     KString& operator=(const KString&other);        //operator=

     KString operator+(const KString &other)const;   //operator+

     bool operator==(const KString&);               //operator==

     char& operator[](unsigned int);               //operator[]

     size_t size(){return strlen(m_data);};

     ~KString(void) {delete[] m_data;}

private:

     char *m_data;

};

inline KString::KString(const char* str)

{

     if (!str)
     {
       m_data = new char[defalut_size];
       memset(m_data, 0, sizeof(m_data));

     }
     else

     {
         m_data = new char[strlen(str)+1];
         strcpy(m_data,str);
     }

}

inline KString::KString(const KString& other)

{

     if(!other.m_data)
     {
       m_data = new char[defalut_size];
       memset(m_data, 0, sizeof(m_data));
     }
     else

     {

         m_data=new char[strlen(other.m_data)+1];

         strcpy(m_data,other.m_data);

     }

}

inline KString& KString::operator=(const KString& other)

{

     if (this!=&other)

     {
        
         if(!m_data && !other.m_data)
         {
            strcpy(m_data,other.m_data);
         }
        
     }

     return *this;

}

inline KString KString::operator+(const KString &other)const

{

     KString newKString;

     if(!other.m_data)

         newKString = *this;

     else if(!m_data)

         newKString = other;

     else

     {

         newKString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];

         strcpy(newKString.m_data,m_data);

         strcat(newKString.m_data,other.m_data);

     }

     return newKString;

}

inline bool KString::operator==(const KString &s)    

{

     if ( strlen(s.m_data) != strlen(m_data) )

         return false;

     return strcmp(m_data,s.m_data)?false:true;

}

inline char& KString::operator[](unsigned int e)

{

     if (e>=0&&e<=strlen(m_data))

         return m_data[e];

}

ostream& operator<<(ostream& os,KString& str)

{

     os << str.m_data;

     return os;

}

 

8.
/****************

  功能:判断是否为素数
  参数:
      n:输入的整数
  返回值:
         true为素数.
  备注:
 
*****************/
bool isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0) return false;
}
return true;
}

/****************

  功能:输出偶数对应的所有素数对.
  参数:
      n:输入的整数
  返回值:
      void.
  备注:
 
*****************/
void guest(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(isPrime(i)&&isPrime(n-i))
printf("%d=%d+%d/n",n,i,n-i);
}
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值