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之 常用的实现多线 ...
随机推荐
- MapReduce库类
Hadoop除了可以让开发人员自行编写map函数和reduce函数,还提供一些常用函数(mapper.reducer和partitioner)的类库,这些类位于 org.apache.hadoop.m ...
- 二十、oracle pl/sql基础
一.pl/sql developer开发工具pl/sql developer是用于开发pl/sql块的集成开发环境(ide),它是一个独立的产品,而不是oracle的一个附带品. 二.pl/sql介绍 ...
- 纯注解快速使用spring IOC容器
使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...
- Unity3d中Dictionary和KeyValuePair的使用
using UnityEngine; using System.Collections; using System.Collections.Generic;public class test : Mo ...
- 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point
给四个数 问能不能算出24点...我的方法比较烂...920ms 差点TLE.应该有更好的方法. #include<stdio.h> #include<string.h> #i ...
- 《JS权威指南学习总结--8.7 函数属性、方法和构造函数》
内容要点: 在JS程序中,函数是值.对函数执行typeof运算会返回字符串 "function",但是函数是JS中特殊的对象.因为函数也是对象,它们也可以拥有属性和方法,就像普 ...
- Sass与Compress实战:第八章
概要:帮助你实现样式表的最佳性能 本章内容: ● 样式表拼接 ● 样式表和资源压缩 ● 减少和并行图片请求的策略 ● 选择器性能和优化策略 1. 测量客户端性能 性能优化的起点和终点都是测量.在第一次 ...
- Animate.css让添加CSS动画像喝水一样容易
在这个年代,你要是不懂一点点css3的知识,你都不好意思说你是个美工.美你妹啊,请叫我前端工程师好不好.呃..好吧,攻城尸...呵呵,作为一个攻城尸,没有点高端大气上档次的东西怎么能行呢,那么css3 ...
- mysql 取出的日期数据缺少时分秒
项目使用的数据库是mysql 持久层框架是hibernate 利用SQLQuery 获取日期的时候缺少时分秒 将 .addScalar("dealTS",StandardB ...
- 【Sort】希尔排序
希尔排序(ShellSort),缩小增量排序,使用希尔增量时最坏运行时间O(n^2),不同的增量会对运行时间产生显著影响. void shellsort(int *nums,int n) { int ...