summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-10-16 22:50:50 +0200
committerOskar <[email protected]>2024-10-16 22:50:50 +0200
commitce6f87441ab2343f7730139f20d25b41e9dd6080 (patch)
tree431dd16b72c4ab3fa947efad172a1238e538eac9
parent1462c1a161b3e986e066da92638fc3e9813498d4 (diff)
some exercises added here that aren't done, will do later
-rw-r--r--9p29.cpp26
-rw-r--r--9p30.cpp13
-rw-r--r--9p31.cpp47
-rw-r--r--9p32.cpp31
-rw-r--r--9p33.cpp14
-rw-r--r--9p34.cpp12
6 files changed, 143 insertions, 0 deletions
diff --git a/9p29.cpp b/9p29.cpp
new file mode 100644
index 0000000..6a89b06
--- /dev/null
+++ b/9p29.cpp
@@ -0,0 +1,26 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 9.29
+ *
+ *
+ */
+
+int main () {
+
+ // vec.size() == 25
+ // vec.resize(100); would add 75 elements (vec.size() == 100)
+ // vec.resize(10); would remove 15 elements (vec.size() == 10)
+ std::vector<int> vec1(25, 0);
+ std::vector<int> vec2(25, 0);
+ std::cout << "(vec1) Original size: " << vec1.size() << std::endl;
+ vec1.resize(100);
+ std::cout << "(vec1) After resize(100): " << vec1.size() << std::endl;
+ std::cout << "------------------------------" << std::endl;
+ std::cout << "(vec2) Original size: " << vec2.size() << std::endl;
+ vec2.resize(10);
+ std::cout << "(vec2) After resize(10): " << vec2.size() << std::endl;
+ return 0;
+}
diff --git a/9p30.cpp b/9p30.cpp
new file mode 100644
index 0000000..980cee6
--- /dev/null
+++ b/9p30.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 9.30
+ *
+ *
+ */
+
+int main () {
+
+ // elements need to have a default constructor
+ return 0;
+}
diff --git a/9p31.cpp b/9p31.cpp
new file mode 100644
index 0000000..38db051
--- /dev/null
+++ b/9p31.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <list>
+#include <forward_list>
+
+/*
+ *
+ * 9.31
+ *
+ * A lot of comments here but i had to do it to clear it up in my head
+ */
+
+int main () {
+
+ std::list<int> vi = {0,1,2,3,4,5,6,7,8,9};
+ auto iter = vi.begin(); // get begin
+ while (iter != vi.end()) { // when begin is not at the end
+ if (*iter % 2 == 1) { // when it's odd
+ iter = vi.insert(iter, *iter); // insert whatever is at *iter before iter and return iterator where the new value was inserted
+
+ ++iter; // advance past what was inserted
+ ++iter; // advance past the element we checked for odd/even(ness)
+ } else // when even
+ iter = vi.erase(iter); // remove the even value, return iterator to element after the erased element
+ }
+
+ std::forward_list<int> fli = {0,1,2,3,4,5,6,7,8,9};
+ auto beg = fli.begin(); // get begin
+ auto bbeg = fli.before_begin(); // get before begin
+ while (beg != fli.end()) { // while beg is not at the end
+ if (*beg % 2 == 1) { // when odd
+ bbeg = fli.insert_after(bbeg, *beg); // insert *beg after bbeg, returns iterator to inserted element
+ ++beg; // advance beg to next element to be checked
+ ++bbeg; // advance bbeg to follow beg one value behind
+ } else // when it's even
+ beg = fli.erase_after(bbeg); // erase element after bbeg (the one at beg). Returns iterator to element after erased one (aka next one to be checked)
+ }
+
+ for(auto &a : vi) {
+ std::cout << a << std::endl;
+ }
+
+ for(auto &a : fli) {
+ std::cout << a << std::endl;
+ }
+
+ return 0;
+}
diff --git a/9p32.cpp b/9p32.cpp
new file mode 100644
index 0000000..06ea9f1
--- /dev/null
+++ b/9p32.cpp
@@ -0,0 +1,31 @@
+#include <vector>
+#include <iostream>
+
+/*
+ *
+ * 9.32
+ *
+ * Seems very much illegal, at least when using g++. But when compiling with clang++ it seems like it just works... What?
+ *
+ * After breaking out gdb and debugging both the clang++ and g++ program it seems like both compilers do different things
+ * g++ increments the iterator, clang++ (maybe does?) does not increment the iterator.
+ */
+
+int main () {
+
+ std::vector<int> vi = {0,1,2,3,4,5,6,7,8,9};
+ auto iter = vi.begin();
+ while (iter != vi.end()) {
+ if (*iter % 2) {
+ iter = vi.insert(iter, *iter++);
+ iter += 2;
+ } else
+ iter = vi.erase(iter);
+ }
+
+ for(auto &a : vi) {
+ std::cout << a << std::endl;
+ }
+
+ return 0;
+}
diff --git a/9p33.cpp b/9p33.cpp
new file mode 100644
index 0000000..d99433c
--- /dev/null
+++ b/9p33.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+
+/*
+ *
+ * 9.33
+ *
+ *
+ */
+
+int main () {
+
+
+ return 0;
+}
diff --git a/9p34.cpp b/9p34.cpp
new file mode 100644
index 0000000..58d5fbf
--- /dev/null
+++ b/9p34.cpp
@@ -0,0 +1,12 @@
+#include <iostream>
+
+/*
+ *
+ * Description
+ *
+ *
+ */
+
+int main () {
+ return 0;
+}