forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_43.cpp
46 lines (41 loc) · 1.28 KB
/
ex9_43.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// ex9_43.cpp
// Exercise 9.43
//
// Created by XDXX on 4/17/2015.
//
// @Brief Write a function that takes three strings, s, oldVal, and newVal.
// Using iterators, and the insert and erase functions replace
// all instances of oldVal that appear in s by newVal.
// Test your function by using it to replace common abbreviations,
// such as “tho” by “though” and “thru” by “through”.
//
// @note This code doesn't compile on GCC. Please use Visual Studio 2015+ or Clang 3.6+
//
// Refactored by Yue Wang Oct, 2015
//
#include <iterator>
#include <iostream>
#include <string>
#include <cstddef>
using std::cout;
using std::endl;
using std::string;
auto replace_with(string &s, string const& oldVal, string const& newVal)
{
for (auto cur = s.begin(); cur <= s.end() - oldVal.size(); )
if (oldVal == string{ cur, cur + oldVal.size() })
cur = s.erase(cur, cur + oldVal.size()),
cur = s.insert(cur, newVal.begin(), newVal.end()),
cur += newVal.size();
else
++cur;
}
int main()
{
string s{ "To drive straight thru is a foolish, tho courageous act." };
replace_with(s, "tho", "though");
replace_with(s, "thru", "through");
cout << s << endl;
return 0;
}