From 77b344e13eae4b4439f96a8d062151da03bf8263 Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 1 Nov 2024 10:44:53 +0100 Subject: been a while, took a break i guess --- 9p33.cpp | 15 +++++++++++++-- 9p34.cpp | 16 +++++++++++++++- 9p35.cpp | 14 ++++++++++++++ 9p36.cpp | 14 ++++++++++++++ 9p37.cpp | 17 +++++++++++++++++ 9p38.cpp | 24 ++++++++++++++++++++++++ 9p39.cpp | 22 ++++++++++++++++++++++ 9p40.cpp | 30 ++++++++++++++++++++++++++++++ 9p41.cpp | 17 +++++++++++++++++ 9p42.cpp | 14 ++++++++++++++ 10 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 9p35.cpp create mode 100644 9p36.cpp create mode 100644 9p37.cpp create mode 100644 9p38.cpp create mode 100644 9p39.cpp create mode 100644 9p40.cpp create mode 100644 9p41.cpp create mode 100644 9p42.cpp diff --git a/9p33.cpp b/9p33.cpp index d99433c..d677d6e 100644 --- a/9p33.cpp +++ b/9p33.cpp @@ -1,4 +1,4 @@ -#include +#include /* * @@ -9,6 +9,17 @@ int main () { - + /* + Iterator invalid. Undefined behavior. + */ + + std::vector v{1, 2, 3, 4, 5}; + + auto begin = v.begin(); + while (begin != v.end()) { + ++begin; + v.insert(begin, 42); + ++begin; + } return 0; } diff --git a/9p34.cpp b/9p34.cpp index 58d5fbf..a8317ef 100644 --- a/9p34.cpp +++ b/9p34.cpp @@ -1,12 +1,26 @@ #include +#include /* * - * Description + * 9.34 * * */ int main () { + + // Prediction: The program will get stuck on the first odd value and keep inserting it indefinitely + std::vector vi = {1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + auto iter = vi.begin(); + while (iter != vi.end()) { + if (*iter % 2) { // when it's odd + iter = vi.insert(iter, *iter); // insert copy of *iter before iter, return iterator to inserted value + std::cout << "inserted value: " << *iter << "\n"; + } + + ++iter; // go one step forwards + } + return 0; } diff --git a/9p35.cpp b/9p35.cpp new file mode 100644 index 0000000..83f54ff --- /dev/null +++ b/9p35.cpp @@ -0,0 +1,14 @@ + +/* + * + * 9.35 + * + * + */ + +int main () { + + // size returns the amount of elements the vector has + // capacity is the elements the vector can grow up to before it has to reallocate + return 0; +} diff --git a/9p36.cpp b/9p36.cpp new file mode 100644 index 0000000..d4ec6fd --- /dev/null +++ b/9p36.cpp @@ -0,0 +1,14 @@ + +/* + * + * 9.36 + * + * + */ + +int main () { + + // No it can not. + // The capacity can be equal to the size but not less than the size. + return 0; +} diff --git a/9p37.cpp b/9p37.cpp new file mode 100644 index 0000000..50a67a1 --- /dev/null +++ b/9p37.cpp @@ -0,0 +1,17 @@ + +/* + * + * 9.37 + * + * + */ + +int main () { + + // Array is fixed size + // Not sure why list does not have capacity. + // From my understanding of manually creating linked lists + // We create each node and connect the previous one to the new one. + // Maybe there is not really a need to allocate for nodes that dont exist? + return 0; +} diff --git a/9p38.cpp b/9p38.cpp new file mode 100644 index 0000000..c396afa --- /dev/null +++ b/9p38.cpp @@ -0,0 +1,24 @@ +#include +#include + +/* + * + * 9.38 + * + * + */ + +int main () { + + std::vector vi = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + std::vector vi2(100); + std::vector vi3; + std::cout << vi.size() << " " << vi.capacity() << std::endl; + std::cout << vi2.size() << " " << vi2.capacity() << std::endl; + for(size_t i = 0 ; i != 1025 ; ++i) { + vi3.push_back(0); + std::cout << vi3.size() << " " << vi3.capacity() << std::endl; + } + + return 0; +} diff --git a/9p39.cpp b/9p39.cpp new file mode 100644 index 0000000..c50c769 --- /dev/null +++ b/9p39.cpp @@ -0,0 +1,22 @@ +#include + +/* + * + * 9.39 + * + * + */ + +int main () { + + /* + vector svec; // Creates vector + svec.reserve(1024); // Reserve 1024 so it can grow 1024 times before reallocating + string word; + while (cin >> word) { // Get strings + svec.push_back(word); + } + svec.resize(svec.size()+svec.size()/2); // We resize again + */ + return 0; +} diff --git a/9p40.cpp b/9p40.cpp new file mode 100644 index 0000000..517dd91 --- /dev/null +++ b/9p40.cpp @@ -0,0 +1,30 @@ +#include +#include + +/* + * + * 9.40 + * + * + */ + +int main () { + + /* + 256 words = 1024 + 512 words = 1024 + 1000 words = 2000 + 1048 words = 2048 + */ + + std::vector svec; + svec.reserve(1024); + std::string word; + while (std::cin >> word) { + svec.push_back(word); + } + + svec.resize(svec.size()+svec.size()/2); + std::cout << svec.capacity() << std::endl; + return 0; +} diff --git a/9p41.cpp b/9p41.cpp new file mode 100644 index 0000000..f96e38e --- /dev/null +++ b/9p41.cpp @@ -0,0 +1,17 @@ +#include +#include + +/* + * + * 9.41 + * + * + */ + +int main () { + + std::vector vc = {'a','b','c','d','e','f','g','h','i','j'}; + std::string s1(vc.cbegin(), vc.cend()); + std::cout << s1 << std::endl; + return 0; +} diff --git a/9p42.cpp b/9p42.cpp new file mode 100644 index 0000000..f25dfa6 --- /dev/null +++ b/9p42.cpp @@ -0,0 +1,14 @@ + +/* + * + * 9.42 + * + * + */ + +int main () { + + // Use .reserve(100) function + + return 0; +} -- cgit v1.2.3