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之 常用的实现多线 ...
随机推荐
- Documention
Object.bool Does the object exist? Object.name Components share the same name with the game object a ...
- cxf 报错:java.lang.NoSuchMethodError: org.apache.ws.commons.schema.XmlSchemaCollection.read(Lorg/w3c/dom/Document;Ljava/lang/String;)
由于没有仔细查看官方提供的文档,由jdk版本不一致导致的出错: http://cxf.apache.org/cxf-316-release-notes.html 自己使用的是jdk1.8. 报Exce ...
- KVM设置DHCP、DNS、Gateway
添加网卡 sudo vi /etc/network/interfaces #添加下面的配置 >>>>>>>> auto tap0 iface tap0 ...
- flume-hdfs sinks报错
flume使用hdfs sinks时候报错:org.apache.flume.EventDeliveryException: java.lang.NullPointerException: Expec ...
- js日期操作时间看板
var nowTime = null;//获取服务器时间function GetTime() { $.ajax({ url:config._domain + "/AjaxAuctionCen ...
- 第四节,Linux基础命令
第四节,Linux基础命令 命令是系统操作员对系统传入的指令,传入指令后回车,系统接收到指令做出相应的行为 1.查看用户位于系统什么位置 [pmd]检查操作用户位于系统的什么位置 命令 ...
- [妙味JS基础]第九课:定时器管理、函数封装
知识点总结 函数封装 回调函数 实例:抖动函数 获取当前的位置 通过数组来实现,一正一负,直到恢复成0为止. 当前位置与数组中各值相加
- 大D实例化model-->调用自定义类方法,大M调用原声model方法
class ContactsModel extends Model{ public function addxxx(){ } } $conmodel = D('contacts','Model'); ...
- PHP中使用CURL(六)
curl常用的几个例子 1.抓取无访问控制文件 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://local ...
- WTL版本ACEdit控件,改写自MFC版,附带源码
自动完成是个很酷也很实用的功能,比如在浏览器地址栏输入几个字母,相关的记录就会在下拉框中陈列出来. 最近在做公司产品UI部分的改善,原版本是MFC做的,我决定用WTL,于是就遇到自动完成控件的问题.遍 ...