forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_43.cpp
75 lines (61 loc) · 2.26 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! @Alan
//!
//! Exercise 9.43:
//! 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”.
//!
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
//!
//! \brief Exercise 9.43
//! @note Using iterators, and the insert and erase functions
//! \return 0 : replace operation happenned ; -1 : otherwise.
//!
int abbrv_handler(std::string&, const std::string&, const std::string&);
int main()
{
std::string s("thru _ thru ");
abbrv_handler(s,"thru","through");
std::cout << s;
return 0;
}
int abbrv_handler(std::string &s, const std::string &oldVal, const std::string &newVal)
{
if(oldVal.size() > s.size()) return -1;
auto it = s.begin();
while(it != s.end())
{
if (*it == *oldVal.begin())
{
//! @note build sub_str from s to compare the oldVal laterly
auto tmp = s.substr(it - s.begin(), oldVal.size());
if (tmp.compare(oldVal) == 0)
{
//! @note Store the offset has been checked
int offSet = it - s.begin();
//! @note Remove [first,last) and update itterator using the return.
it = s.erase(it, it + oldVal.size());
//! @note No proper iterator returned by all overloaded insert memeber.
s.insert(it, newVal.begin(), newVal.end());
//! @note Manually, move the iterator to point to the last of the added element:
//! Assuming "through" is newly added:
//! ********through******
// ^
//! above is where it points to.
//! by the statement ++it, it will move to the next element as shown below:
//! ********through******
// ^
it = s.begin() + offSet + oldVal.size() - 1;
}
}
++it;
}
return 0;
}