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
- Introduction to Programming with c++ 10-2 replace strings and split strings
- Parsing Strings with split
- Introduction to Programming with c++ 2-10 类型转换学习
- Introduction to Computing and Programming with Java : A Multimedia Approach
- Introduction to Programming in C/C with Vim
- 482 C. Game with Strings
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- Introduction to Programming in C/C++ with Vim
- Introduction to Programming in C/C++ with Vim
- Converting numbers to strings and strings to numbers
- Converting numbers to strings and strings to numbers
- Strings in C and C++
- 477 C. Dreamoon and Strings
- 【CODEFORCES】 C. Dreamoon and Strings
- Introduction to C Memory Management and C++ Object-Oriented Programming
- 测量中的坐标与时间系统1.3(在大地测量学中)
- 2016/10/14
- 自动更新微信access token
- OpenJudge noi 7624山区建小学
- C++中的程序预处理
- Introduction to Programming with c++ 10-2 replace strings and split strings
- scala-映射map
- POJ_3007_Organize Your Train part II
- 使用pelican搭建一个Jupyter Notebook数据科学博客
- C语言数据类型与变量:(二)
- python爬虫练手二
- POJ_3452_Railway Transportation
- 27个Jupyter Notebook小提示与技巧
- 面向对象