Introduction to Programming with c++ 10-2 replace strings and split strings

课本学习

P376,find函数以及replace函数。

find函数及其用法:

  • unsigned find( string s ) // Returns the position of the first matching substring s
  • unsigned find( string s, int index ) // Return the position of the first matching substring s starting at or from the position index

replace函数及其用法:

  • string replace( int index, int n, string s ) // Replace the n characters starting at position position index in this string with the string s

问题

Write the following function that replaces the occurences of a substring oldSubString with a new substring newSubString in the string s

思路

1.start_index = 0;
2.循环:ret = s.find( start_index, old_str ), ret != std::npos
    2.1.替换new_str
    2.2.start_index = ret + sz_new_str
3.返回s

代码

#include <iostream>
#include <string>

bool replace_string( std::string& s, const std::string& old_substr, const std::string& new_substr ); // return true if s is changed, and otherwise, it returns false.


int main( void )
{
    std::string s, old_str, new_str;
    std::cout << "Enter string s, oldSubStr, and newSubStr: ";
    std::cin >> s >> old_str >> new_str;

    bool ret = replace_string( s, old_str, new_str );
    if( ret )
        std::cout << "The replace string is " << s << std::endl;
    else
        std::cout << "No matches" << std::endl;

    return 0;
}

bool replace_string( std::string& s, const std::string& old_substr, const std::string& new_substr )
{
    int start_index = 0;
    int ret = 0;

    int sz_old = old_substr.size();
    int sz_new = new_substr.size();

    bool flag = false;
    while( ret = s.find( old_substr, start_index ), ret != std::string::npos )
    {
        flag = true;
        s.replace( ret, sz_old, new_substr );
        start_index += sz_new;
    }

    return flag;
}

问题

实现python当中的split函数,即给定字符串以及分隔符,给出提取后的字符串。需要注意:

    实现split()函数。
    按照结尾符号,进行分割。
    当然,对于空格是很容易的。但是对于',',则不能用stringstream的办法。
    因为只有空白符才是stringstream的分隔符。

下面给出两个版本的代码,第一个代码对于不存在空串的情形不能很好地处理。第二个版本则解决了这个问题。

#include <iostream>
#include <string>
#include <vector>

std::vector< std::string > split( const std::string& line, char delimeter = ',' );
std::vector< std::string > split1( const std::string& line, char delimeter = ',' );

int main( void )
{
    std::string test_str = ",,,";
    std::vector< std::string > ret = split1( test_str );

    int sz = ret.size();
    std::cout << sz << std::endl;
    for( int i = 0; i < sz; ++i )
    {
        std::cout << ret[i] << std::endl;
    }


    return 0;
}

这个版本对于不存在空串的情形可以处理,本质是模式识别

std::vector< std::string > split( const std::string& line, char delimeter )
{
    typedef std::string::const_iterator const_iter;
    const_iter b = line.begin();
    const_iter e = line.end();

    std::vector<std::string> ret;
    while( b != e )
    {
        // find the begin
        while( b != e && *b == delimeter ) ++b;

        if( b < e )
        {
            // find the end
            const_iter after = b;
            while( after != e && *after != delimeter ) ++after;

            // generate a pattern
            ret.push_back( std::string(b, after) );

            b = after;
        }
    }
    return ret;
}

注意结尾是空串的情形,比如”hello,world,hello,”。这里面应该是4个字符串,最后一个是空串

std::vector< std::string > split1( const std::string& line, char delimeter )
{
    std::vector< std::string > ret;
    typedef std::string::const_iterator const_iter;
    const_iter b = line.begin();
    const_iter e = line.end();


    while( b < e )
    {
        const_iter after = b;
        while( after < e && *after != delimeter ) ++after;
        if( b < after )
        {
            ret.push_back( std::string( b, after ) );
            b = (after < e)?(after + 1):after;
        }
        else
        {
            ret.push_back(std::string(""));
            b = (after < e)?(after + 1):after;
        }
    }

    if(*(b-1) == delimeter) // 最后一个符号是","的情形
        ret.push_back( "" );

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值