C++primer 9.43
题目要求:编写一个函数,接受三个string参数s,oldVal和newVal。使用迭代器及insert和erase函数将s中所有oldVal替换为newVal。测试你的程序,用它替换通用的简写形式,如,将"tho"替换为"though",将"thru"替换为"though"。
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std; void find_replace(string s, string oldValue, string newValue)
{
if (s.empty() || oldValue.empty() || newValue.empty())
{
cout << "Error" << endl;
return;
}
if (s.size() < newValue.size())
{
cout << "Error" << endl;
return;
} string word;
vector<string>vec; //分割string
istringstream stream(s);
{
while(stream >> word)
vec.push_back(word);
} vector<string>::iterator it1 = vec.begin(); while (it1 != vec.end())
{
if (*it1 == oldValue)
{
string::iterator it2 = (*it1).begin();
it2 = (*it1).erase(it2, it2+oldValue.size());
(*it1).insert(it2, newValue.begin(), newValue.end());
}
else
{
string::iterator it3 = (*it1).begin();
string::iterator it4 = oldValue.begin();
while (it3 != (*it1).end())
{
if ((*it3) == (*it4))
{
string sub = (*it1).substr(it3-(*it1).begin(), oldValue.size());
if (sub == newValue)
{
unsigned offset = it3 - (*it1).begin();
it3 = (*it1).erase(it3, it3+oldValue.size());
(*it1).insert(it3, newValue.begin(), newValue.end());
it3 = (*it1).begin() + offset + newValue.size() - 1;
}
}
it3++;
}
}
it1++;
} for (auto i = vec.begin(); i != vec.end(); i++)
cout << *i << endl; } int main()
{
//略 return 0;
}
C++primer 9.43的更多相关文章
- c++ primer 5th 练习3.43
#include <iostream> using namespace std; int main() { ][]={,,,,,,,,,,,}; /* for(int (&i)[4 ...
- 转载:看c++ primer 学习心得
学习C++ Primer时遇到的问题及解释 chenm91 感觉: l 啰嗦有时会掩盖主题:这本书确实有些啰嗦,比如在讲函数重载的时候,讲了太长一大段(有两节是打了*号的,看还是不看 ...
- C++ Primer中文版(第5版)
<C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼) Josee Lajoie(约瑟 拉乔伊) Barbar ...
- c++ primer plus 第6版 部分一 1-4章
c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c cc cxx C cpp ...
- C Primer Plus 学习体会
本月刚刚开始学习<C primer plus>,之前课上草草学过一遍,讲到指针就结束了.现在重新开始看感觉难度不大只是刚开始接触有些语言细节比较琐碎.学习这一周的体会如下: 诸多前辈推荐的 ...
- C++ Primer Plus 第六版笔记
C++ Primer Plus 第六版笔记 关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C+ ...
- 《3D Math Primer for Graphics and Game Development》读书笔记2
<3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...
- 《3D Math Primer for Graphics and Game Development》读书笔记1
<3D Math Primer for Graphics and Game Development>读书笔记1 本文是<3D Math Primer for Graphics and ...
- Java多线程系列目录(共43篇)
最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...
随机推荐
- Linux文件挂载命令mount
在linux系统中硬盘.u盘.光驱等其他设备都需要挂载后才能正常使用.下面是对挂载命令mount使用方法的一些总结. 文件挂载命令mountmount [-t 文件系统类型][-L卷标名][-o特殊选 ...
- JSONObject处理java.util.Date
JSONObject的内容为: {"userId":"A000004FFDCE14","userName":"好好干g" ...
- jfianl返回自定义的404页面
public class MyErrorRenderFactory implements IErrorRenderFactory{ public Render getRender(int errorC ...
- JZ2440串口打印字符作为调试
/* * 初始化UART0 * 57600,8N1,无流控 */ void uart0_init(void) { GPHCON |= 0xa0; // GPH2,GPH3用作TXD0,RXD0 GPH ...
- vs2015下载及预览与发布
VS2015 RC发布下载,通用Windows平台必备神器! 几个月前微软发布了Visual Studio 2015的技术预览版本,之后又先后发布了6个更新版本.如今,微软已在其官方页面中公布了最新开 ...
- C#_FindWindow
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Java中的DateFormatter
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul;07 w 年中的周数 Number 27 W ...
- 印象烟大PPT大赛
下面为获奖人员 王志恒一等奖 姜云飞.任子仪二等奖 田正相,庄棫麟,陈德昊三等奖.
- Spring Security(09)——Filter
目录 1.1 Filter顺序 1.2 添加Filter到FilterChain 1.3 DelegatingFilterProxy 1.4 FilterChainPr ...
- JVM基础02-class文件
一.class文件结构 介绍之前,请下载一个Bytecode工具,例如byte code viewer或者Java Bytecode Editor,我用的是后者Java Bytecode Editor ...