diff options
author | Oskar <[email protected]> | 2024-08-11 17:30:28 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-11 17:30:28 +0200 |
commit | b45c0d29ee690c8b436a33581d3b108e6064b5a7 (patch) | |
tree | 90b0df74cb9be982d35b3287f105c2e67f117ebb | |
parent | c6a3ec820eeb105752b833c9f5e44f0244a41c6d (diff) |
more confusion
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | 3p26.cpp | 54 | ||||
-rw-r--r-- | README | 7 | ||||
-rw-r--r-- | binarysearch-example.cpp | 56 | ||||
-rw-r--r-- | iterator-test-3.cpp | 34 |
5 files changed, 152 insertions, 0 deletions
@@ -1,2 +1,3 @@ bin/ redirection/ +TODO diff --git a/3p26.cpp b/3p26.cpp new file mode 100644 index 0000000..3535b19 --- /dev/null +++ b/3p26.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.26 + * + * We cannot use '+' to add 2 iterators + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + vector<int> in = {1, 4, 23, 35, 36, 111, 122, 135, 239, 345, 365, 399, 401, 477, 480, 592, 728, 777, 824, 928, 983, 1002, 1023, 1209}; + int sought = 0; + for(auto vt : in) { + cout << vt << " "; + } + + cout << endl; + if(cin >> sought) { + } else { + return -1; + } + + auto beg = in.begin(), end = in.end(); + auto mid = in.begin() + (end - beg)/2; // 12 + while (mid != end && *mid != sought) { + if (sought < *mid) { + end = mid; + } else { + beg = mid + 1; + } + + cout << *mid << endl; + mid = beg + (end - beg)/2; + } + + if(mid != end) { + cout << "found: " << *mid << endl; + } else { + cout << "could not find: " << sought << endl; + } + + return 0; +} @@ -0,0 +1,7 @@ +My exercises for C++ Primer 5th edition. + +Not all exercises are included. + +Some exercises may or may not follow the directions completely, +it could be because of compiler flags that i have enabled and i have to make changes. +Or it could be because i wanted to add some extra small things to make it easier. diff --git a/binarysearch-example.cpp b/binarysearch-example.cpp new file mode 100644 index 0000000..dcf6809 --- /dev/null +++ b/binarysearch-example.cpp @@ -0,0 +1,56 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * Binary search program from the book, added some + * extra stuff to make the program a little bit more + * complete. Also added some brackets to follow my preffered style. + * Maybe it would have been nice to make a randomly generated sorted + * vector but whatever this example is good enough... + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + vector<int> in = {1, 4, 23, 35, 36, 111, 122, 135, 239, 345, 365, 399, 401, 477, 480, 592, 728, 777, 824, 928, 983, 1002, 1023, 1209}; + int sought = 0; + for(auto vt : in) { + cout << vt << " "; + } + + cout << endl; + if(cin >> sought) { + } else { + return -1; + } + + auto beg = in.begin(), end = in.end(); + auto mid = in.begin() + (end - beg)/2; + while (mid != end && *mid != sought) { + if (sought < *mid) { + end = mid; + } else { + beg = mid + 1; + } + + mid = beg + (end - beg)/2; // new midpoint + } + + if(mid != end) { + cout << "found: " << *mid << endl; + } else { + cout << "could not find: " << sought << endl; + } + + return 0; +} diff --git a/iterator-test-3.cpp b/iterator-test-3.cpp new file mode 100644 index 0000000..09cf159 --- /dev/null +++ b/iterator-test-3.cpp @@ -0,0 +1,34 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" +/* + * + * Pretty much same as last iterator test + * Still a bit confused about this stuff. + * Especially the fact that you cant add two iterators + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + + vector<int> vec = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + auto it1 = vec.begin(); // points at 10 in the vector + it1 += 9; // we go 9 places forwards - 20 - 30 - 40 - 50 - 60 - 70 - 80 - 90 - 100. + // it1 now points to 100 + cout << *it1 << endl; + auto it2 = vec.begin(); + auto diff = it1 - it2; + auto it3 = vec.begin(); + it3 += diff; + cout << diff << " < diff" << endl; + cout << *it3 << endl; + return 0; +} |