c/c++经典积累

  1. 用C实现自己的memcpy
    先后给出一段代码
     #include <stdio.h>
    #include <iostream>
    using namespace std;
    void* mymemcpy( void *dest, const void *src, size_t count )
    {
        char* pdest = static_cast<char *>(dest);
        const char* psrc = static_cast<const char *>( src );
        if( pdest>psrc && pdest)   
     {
            for( size_t i=count-1; i!=-1; --i )
                    pdest[i] = psrc[i];
        }
        else
        {
            for( size_t i=0; i< count; i++)
       pdest[i] = psrc[i];
        }
        return dest;
    }
    int main(int argc,char *argv[])
    {
     char str[] = "0123456789";
        mymemcpy( str+1, str+0, 9 );
        cout << str << endl;
        system( "Pause" );
        return 0;
    }
    看过上面的代码,其实memecpy即顺序从一个内存空间逐个字节拷贝,原理很简单,但是如果没有if( pdest>psrc && pdest)    这个条件判断,可能得不到预想的结果.

    首先从src地址开始的count个字节和从dest开始的count个字节很可能会出现交叉,如果出现交叉,那么就要注意拷贝的顺序,不然就得不到理想的结果;
    情况一:
    比如,如果src=10000,dest=10005,count=10,那么如果从从src[0]开始到dest[0]开头这样拷贝,那么在dest[5]=src[5]的时候,其实这个时候的src[5]就是最开始的src[0],原来src[5]这个位置的原始数据已经在第一次拷贝的时候被覆盖了;后面到dest[9]=src[9]也是类似的情况,被覆盖;所以在dest中的结果将是src[0]-src[4]的两分;
    情况二:
    如:src=10000,dest=99995,count=10,情况类同,就必须从dest[0]=src[0]到dest[9]=src[9]顺序拷贝才能得到正确的结果;如果dest[9]=src[9]到dest[0]=src[0]拷贝,那么就会出现类似上面覆盖的问题;
  2. 用C实现自己的atol
    long myatol(char *src)
    {
     int len = 0;
     int i = 0;
     int j = 0;
     long ltemp = 0;
     long lRet = 0;
     if(src == NULL)
      return 0;
     len = strlen(src);
     for(i = len-1; i>=0; i--)
     {
      ltemp = src[i]-'0';
      for(j = 0; j<len-i-1; j++)
       ltemp *=10;
      lRet += ltemp;
     }
     return lRet;
    }
  3. getopt应用
    表头文件  #include<unistd.h>或者<getopt.h> ,其实在unistd.h中包含了getopt.h
    定义函数  int getopt(int argc,char * const argv[ ],const char * optstring);
    函数说明  getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数(参数值),全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
    返回值  如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
    示例代码,文件名testopt.cpp
    #include <stdio.h>
    #include <getopt.h>
    //#include <unistd.h>
    int main(int argc,char **argv)

     int ch;
     opterr = 0;
     while((ch = getopt(argc,argv,"a:bcde"))!= -1)
     switch(ch)
     {
      case 'a':
       printf("option a=%s/n",optarg);
       break;
      case 'b':
       printf("option b:%c/n",ch);
       break;
      break;
       default:
       printf("other option :%c/n",ch);
     }
     
     printf("optopt +%c/n",optopt);
    }
    运行测试:
    测试命令一:./testopt -a 123
    输出:
    option a=123
    optopt +?
    说明:由于参数optstring中a后有:,表示将有相关的参数值,所以optarg指向参数值123

    测试命令二:./testopt -a
    输出:
    other option :?
    optopt +a
    说明:由于a:表示a参数后面应该有参数值,实际情况却没有,所以ch指向?表示没有匹配的参数格式。

    测试命令三:./testopt -d
    输出:
    other option :d
    optopt +?
    说明:由于d参数在optstring串中,所以是-d是接受参数之一。

    测试命令四: ./testopt -a 123 -a 789
    输出:
    option a=123
    option a=789
    optopt +?
    说明,程序将顺序读出参数串,一般是最后一个参数有效。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值