zoukankan      html  css  js  c++  java
  • C++字符串处理函数 沧海

    C++失败题

    class BYTE
    main(){
    BYTE * pByte = new BYTE[4];
    for(int i = 0; i < 4; i++)
    {
      *pByte = i;
      pByte++;
    }
    }
    内存错误,未初始化?
    实现字符串拷贝函数strcpy()
    #include <iostream>
    using namespace std;
    char* strcpy(char* dest, const char *src )
    {
    // char* pdest = static_cast<char*>(dest);
    // const char* psrc = static_cast<const  char*>(src);
    if((dest==NULL)||(src==NULL))
    throw"error";
    char* strdest = dest;
    while((*dest++ = *src++)!='\0');
    return strdest;
    }

    int main()
    {
     char* sdest = (char*)malloc(strlen(str));
     char str[] = "0123456789";
     strcpy( sdest, str);
     cout<< sdest<<endl;
     return 0;
     free(sdest);
    }
     
    实现内存拷贝函数memcpy()

    #include <iostream>
    using namespace std;
    void* memcpy(void * dest, const void *src, size_t count )
    {
     char* pdest = static_cast<char*>(dest);
     const char* psrc = static_cast<char*>(src);
     if(pdest>psrc && pdest<psrc+count)
     {
      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()
    {
     char src[]= "0123456789";
     memcpy( str+1; str+0; 9);
     cout<< str<<endl;
     return 0;
    }
     
    二、编写一函数实现子字符串查找
    char *strstr(char* str, char * substr)
     
    main()
    {
     char b[]="0123456789ABCDEF";
     int c[64],d,i=0,base=16;
     long n;
     scanf("%ld",&n);
     do{
       c[i]=   ;i++;n=n/base;
     }while(n!=0);
      for(--i;i>=0;--i)
      {d=c[i];printf("%c",b[d]);}
      getch();
    }
    int AnsiString::Pos(const AnsiString& subStr)
    {
     int len=subStr.Length();
     int p=0;
     for(int i=0;i<DataLen;i++)
     {
      if(i+1+len<=DataLen)
      {
       if(SubString(i+1,len)==subStr)
       {
        p=i+1;
        break;   
       }
      }
      else
      {
       break;
      }
     }
     return p;
    }
     
    AnsiString AnsiString::SubString(int index, int count)
    {
     index--;
     if(index+count>DataLen)
     {
      count=DataLen-index;
     }
     char* tmp=new char[count+1];
     memcpy(tmp,Data+index,count);
     tmp[count]=0;
     AnsiString Result=tmp;
     delete []tmp;
     return Result;
    }
    XOR eax,eax(156252207) 22:00:39
    pos函数就是

    struct A
    {
       B*pB;
    }
    struct B
    {
       A* pA;
    }
    一、实现编译通过:
    预声明
    struct B;
    struct A
    {
       B * pB;
    };
    struct B
    {
       A* pA;
    };
    专注于企业信息化,最近对股票数据分析较为感兴趣,可免费分享股票个股主力资金实时变化趋势分析工具,股票交流QQ群:457394862
  • 相关阅读:
    TypeScript 里的 module 概念
    SAP Spartacus Definition of Done
    SAP Commerce Cloud 新一代 UI Spartacus 和 Customer Data cloud 的集成
    关于 SAP Spartacus 和 SmartEdit 集成的问题
    Linux Boot,Kernel 和 Service 介绍
    Linux 的发展历史,设计哲学和一些常用的术语介绍
    SAP Fiori Elements 公开课第三单元学习笔记
    SAP Fiori Elements 公开课第二单元视频的台词和课程主要内容
    SAP Fiori Elements 公开课第二单元学习笔记:Fiori Elements 架构
    qt-事件处理的方式
  • 原文地址:https://www.cnblogs.com/omygod/p/554696.html
Copyright © 2011-2022 走看看