summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-10-15 17:05:33 +0200
committerOskar <[email protected]>2024-10-15 17:05:33 +0200
commit7db4642f463244fdbacd2b20f261398c9de38fd9 (patch)
treea6f79131c824e489518749b17b7f4e452046e36b
parent428d857359b5d8b639ca41b03ca8fc8b3d143a50 (diff)
more
-rw-r--r--9p23.cpp32
-rw-r--r--9p24.cpp18
-rw-r--r--9p25.cpp31
-rw-r--r--9p26.cpp50
-rw-r--r--9p27.cpp36
-rw-r--r--9p28.cpp53
6 files changed, 220 insertions, 0 deletions
diff --git a/9p23.cpp b/9p23.cpp
new file mode 100644
index 0000000..432e77f
--- /dev/null
+++ b/9p23.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 9.23
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> c = {1};
+ if (c.size() == 1) {
+ // val and val2 are copies of the value of the first element in c
+ auto val = *c.begin(), val2 = c.front();
+ // val3 and val4 are copies of the of the last element in c
+ auto last = c.end();
+ auto val3 = *(--last); // can’t decrement forward_list iterators
+ auto val4 = c.back(); // not supported by forward_list
+
+ std::cout << val << "\n"
+ << val2 << "\n"
+ << val3 << "\n"
+ << val4 << "\n";
+ if(val || val2 || val3 || val4) {}
+
+ }
+
+
+ return 0;
+}
diff --git a/9p24.cpp b/9p24.cpp
new file mode 100644
index 0000000..23b0e0e
--- /dev/null
+++ b/9p24.cpp
@@ -0,0 +1,18 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 9.24
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> a;
+ std::cout << a.at(0) << std::endl; //Exception
+ std::cout << a.front() << std::endl; // Segfault
+ std::cout << a.back() << std::endl; // Segfault
+ return 0;
+}
diff --git a/9p25.cpp b/9p25.cpp
new file mode 100644
index 0000000..0a14d6c
--- /dev/null
+++ b/9p25.cpp
@@ -0,0 +1,31 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 9.25
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> slist;
+ auto elem1 = slist.begin();
+ auto elem2 = slist.end();
+ auto elem1end = slist.end();
+ auto elem2end = slist.end();
+ // All iterators above are the same
+ // And no, nothing happens.
+ if(elem1 == elem2) {
+ std::cout << "elem1 == elem2" << std::endl;
+ elem1 = slist.erase(elem1, elem2);
+ }
+
+ if(elem1end == elem2end) {
+ std::cout << "elem1end == elem2end" << std::endl;
+ elem1end = slist.erase(elem1end, elem2end);
+ }
+
+ return 0;
+}
diff --git a/9p26.cpp b/9p26.cpp
new file mode 100644
index 0000000..2bef2ef
--- /dev/null
+++ b/9p26.cpp
@@ -0,0 +1,50 @@
+#include <iostream>
+#include <vector>
+#include <list>
+
+/*
+ *
+ * 9.26
+ *
+ *
+ */
+
+int main () {
+
+ int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
+ std::list<int> li;
+ std::vector<int> vi;
+ for(decltype(std::size(ia)) i = 0 ; i != std::size(ia) ; ++i) {
+ li.push_back(ia[i]);
+ vi.push_back(ia[i]);
+ }
+
+ auto beg = li.begin();
+ for( ; beg != li.cend() ; ) {
+ if(*beg % 2 == 1) {
+ beg = li.erase(beg);
+ } else {
+ ++beg;
+ }
+ }
+
+ auto begv = vi.begin();
+ for( ; begv != vi.cend() ; ) {
+ if(*begv % 2 == 1) {
+ ++begv;
+ } else {
+ begv = vi.erase(begv);
+ }
+ }
+
+ for(auto &a : li) {
+ std::cout << a << "\n";
+ }
+
+ std::cout << std::endl;
+ for(auto &a : vi) {
+ std::cout << a << "\n";
+ }
+
+ return 0;
+}
diff --git a/9p27.cpp b/9p27.cpp
new file mode 100644
index 0000000..07913f8
--- /dev/null
+++ b/9p27.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+#include <forward_list>
+
+/*
+ *
+ * 9.27
+ *
+ *
+ */
+
+int main () {
+
+ std::forward_list<int> flst = {0,1,2,3,4,5,6,7,8,9};
+ for(auto &a : flst) {
+ std::cout << a << "\n";
+ }
+
+ auto bbeg = flst.before_begin();
+ auto beg = flst.begin();
+ auto end = flst.cend();
+ while(beg != end) {
+ if(*beg % 2 == 1) {
+ beg = flst.erase_after(bbeg);
+ } else {
+ bbeg = beg;
+ ++beg;
+ }
+ }
+
+ std::cout << std::endl;
+ for(auto &a : flst) {
+ std::cout << a << "\n";
+ }
+
+ return 0;
+}
diff --git a/9p28.cpp b/9p28.cpp
new file mode 100644
index 0000000..3098381
--- /dev/null
+++ b/9p28.cpp
@@ -0,0 +1,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;
+}