zoukankan      html  css  js  c++  java
  • C++ 删除字符串的两种实现方式

    C++实现删除给定字符串的给定字符串思路主要有这么几种实现方式:

    1.KMP算法
    2.用STL的string的 find,然后用erase
    3.用C的strstr找到字串位置,然后用strncpy写到新串中
    4.用boost库,用正则表达式

    测试过的完整代码:

    第一种方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    #include<iostream>
    #include <string>
    using namespace std;
     
    void deletestr(const char *str, const char* sub_str, char *result);
     
    int main()
        char str[100],sub[100];
        cin>>str;
        cin>>sub;
        char result;
        deletestr(str,sub,&result);
        return 0;
    }
     
    void deletestr(const char *str, const char* sub_str, char *result)
    {  
        int sublen = 0;         //获得子串的长度
        const char *t = sub_str;
        while(*t++ != '')
        {
            sublen++;
        }
     
        int pos = 0;
        int pp = 0;
        int repos = 0; // 结果子串的索引
        while(*(str + pos) != '')
        {
            char t = *(str + pos);
            if(t == *(sub_str + pp)) // 重复子串起始位置
            {
                *(result + repos) = t;
                repos++;
     
                if(pp < sublen - 1) // 还未完全重复
                {
                    pp++;
                }
                else if(pp == sublen - 1) // 完全重复了
                {
                    pp = 0;
                    repos -= sublen; // 回溯下标位置
                }           
            }
            else// 不是一样的字符
                *(result + repos) = t;
                repos++;
            }
            pos++;
        }
        *(result + repos) = '';
        cout<<result<<endl;
    }

     第二种方法,用STL

    个人感觉很简单方便

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #include<iostream>
    #include <string>
    using namespace std;
     
    void deletesub(string &str,const string &sub,int n);
    int main()
        string str,sub;
        cin>>str;
        cin>>sub;
        int n=sub.size();
        deletesub(str,sub,n);
        return 0;
    }
    void deletesub(string &str,const string &sub,int n)
    {
        int m,flag=0,num=0;           //num是子串出现的次数
       while(flag==0)
       {
            m=str.find(sub);
            if(m<0)
                flag=1;
            else
            {
              str.erase(m,n);           //删除子串
              num++;
            }
       }
    //  cout<<num<<endl;          //子串出现的次数
        cout<<str<<endl;         // 输出删除后的字符串  
    }
  • 相关阅读:
    合并区间
    判断字符串是否是IP
    Python -- 异常处理
    python -- 双下方法
    python -- 判断函数和方法
    python -- 面向对象:反射
    Python -- 面向对象:类的成员
    Python -- 面向对象:类的约束
    Python -- 面向对象的三大特性及深入了解super()
    Python -- mro算法
  • 原文地址:https://www.cnblogs.com/whwywzhj/p/8618307.html
Copyright © 2011-2022 走看看