题目要求:编写一个函数,接受三个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的更多相关文章

  1. c++ primer 5th 练习3.43

    #include <iostream> using namespace std; int main() { ][]={,,,,,,,,,,,}; /* for(int (&i)[4 ...

  2. 转载:看c++ primer 学习心得

    学习C++ Primer时遇到的问题及解释 chenm91 感觉: l          啰嗦有时会掩盖主题:这本书确实有些啰嗦,比如在讲函数重载的时候,讲了太长一大段(有两节是打了*号的,看还是不看 ...

  3. C++ Primer中文版(第5版)

    <C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼)    Josee Lajoie(约瑟 拉乔伊)    Barbar ...

  4. c++ primer plus 第6版 部分一 1-4章

    c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c   cc   cxx     C    cpp     ...

  5. C Primer Plus 学习体会

    本月刚刚开始学习<C primer plus>,之前课上草草学过一遍,讲到指针就结束了.现在重新开始看感觉难度不大只是刚开始接触有些语言细节比较琐碎.学习这一周的体会如下: 诸多前辈推荐的 ...

  6. C++ Primer Plus 第六版笔记

    C++ Primer Plus 第六版笔记 关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C+ ...

  7. 《3D Math Primer for Graphics and Game Development》读书笔记2

    <3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...

  8. 《3D Math Primer for Graphics and Game Development》读书笔记1

    <3D Math Primer for Graphics and Game Development>读书笔记1 本文是<3D Math Primer for Graphics and ...

  9. Java多线程系列目录(共43篇)

    最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...

随机推荐

  1. Kettle 使用相关

    Kettle 作为国外的一款开源ETL软件,要使用了

  2. android 操蛋的gradle

    首先看语法: -include {filename} 从给定的文件中读取配置参数 -basedirectory {directoryname} 指定基础目录为以后相对的档案名称 -injars {cl ...

  3. android使用shape做selector按钮按下和弹起的动画

    平时效果:   按下效果: selector代码: <?xml version="1.0" encoding="utf-8"?> <selec ...

  4. C#Stopwatch的使用,性能测试

    一,先开启开始或继续测量某个时间间隔的运行时间,然后停止,最后重置时间,输出. using System; using System.Collections.Generic; using System ...

  5. 文本变语音引擎 ekho

    https://github.com/donaldlee2008/ekho https://www.oschina.net/p/ekho

  6. php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间

    权声明:本文为博主原创文章,未经博主允许不得转载. //这个星期的星期一 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否 ...

  7. java 邮件发送 apache commons-email

    package com.sun.mail;import org.apache.commons.mail.Email;import org.apache.commons.mail.EmailExcept ...

  8. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  9. tftp常用命令

    root@hbg:/# tftpBusyBox v1.22.1 (2015-12-18 15:33:52 CST) multi-call binary. Usage: tftp [OPTIONS] H ...

  10. Vultr VPS测试IP $5/月KVM-512MB/15G SSD/1T

    vultr主要卖点是:SSD硬盘.超低价格.全球13个机房.10Gb服务器带宽.3GHz CPU.性能优异.vultr vps详细评测可参考这篇文章. 以下是vutlr vps官方提供的测试IP地址: ...