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

来源:互联网 发布:数据录入公司 编辑:程序博客网 时间:2024/05/29 19:29

课本学习

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_str3.返回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