summaryrefslogtreecommitdiff
path: root/9p28.cpp
blob: 3098381c98e4c10d649e5115df00db5a8b3d2afb (plain)
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
#include <iostream>
#include <forward_list>

/*
 *
 * 9.28
 *
 *
 */

std::forward_list<std::string> &ch9e28(std::forward_list<std::string> &fl, std::string findme, std::string s) {

	bool success = false;
	auto beg = fl.begin();
	auto end = fl.cend();
	while(beg != end) {
		if(*beg == findme) {
			beg = fl.insert_after(beg, s);
			success = true;
			break;
		} else {
			++beg;
		}
	}

	if(success == false) {
		auto sbeg = fl.begin();
		auto sbbeg = fl.before_begin();
		for(; sbeg != end ; ++sbeg, ++sbbeg) {
			
		}

		fl.insert_after(sbbeg, s);
		
	}
	
	return fl;
}

int main () {

	/*
	  My solution felt a bit hacky but it works, whatever.
	 */
	std::forward_list<std::string> flm = {"HELLO", "AA", "YO", "HELL"};
	std::string s = "HELLOafter";
	ch9e28(flm, "HELLO", s);
	for(auto &a : flm) {
		std::cout << a << std::endl;
	}
	
	return 0;
}