diff options
author | Oskar <[email protected]> | 2024-11-01 10:44:53 +0100 |
---|---|---|
committer | Oskar <[email protected]> | 2024-11-01 10:44:53 +0100 |
commit | 77b344e13eae4b4439f96a8d062151da03bf8263 (patch) | |
tree | c7ed835678354e1877383884f7c061b5930b7328 | |
parent | ce6f87441ab2343f7730139f20d25b41e9dd6080 (diff) |
been a while, took a break i guess
-rw-r--r-- | 9p33.cpp | 15 | ||||
-rw-r--r-- | 9p34.cpp | 16 | ||||
-rw-r--r-- | 9p35.cpp | 14 | ||||
-rw-r--r-- | 9p36.cpp | 14 | ||||
-rw-r--r-- | 9p37.cpp | 17 | ||||
-rw-r--r-- | 9p38.cpp | 24 | ||||
-rw-r--r-- | 9p39.cpp | 22 | ||||
-rw-r--r-- | 9p40.cpp | 30 | ||||
-rw-r--r-- | 9p41.cpp | 17 | ||||
-rw-r--r-- | 9p42.cpp | 14 |
10 files changed, 180 insertions, 3 deletions
@@ -1,4 +1,4 @@ -#include <iostream> +#include <vector> /* * @@ -9,6 +9,17 @@ int main () { - + /* + Iterator invalid. Undefined behavior. + */ + + std::vector<int> v{1, 2, 3, 4, 5}; + + auto begin = v.begin(); + while (begin != v.end()) { + ++begin; + v.insert(begin, 42); + ++begin; + } return 0; } @@ -1,12 +1,26 @@ #include <iostream> +#include <vector> /* * - * Description + * 9.34 * * */ int main () { + + // Prediction: The program will get stuck on the first odd value and keep inserting it indefinitely + std::vector<int> 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 <iostream> +#include <vector> + +/* + * + * 9.38 + * + * + */ + +int main () { + + std::vector<int> vi = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + std::vector<int> vi2(100); + std::vector<int> 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 <iostream> + +/* + * + * 9.39 + * + * + */ + +int main () { + + /* + vector<string> 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 <iostream> +#include <vector> + +/* + * + * 9.40 + * + * + */ + +int main () { + + /* + 256 words = 1024 + 512 words = 1024 + 1000 words = 2000 + 1048 words = 2048 + */ + + std::vector<std::string> 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 <iostream> +#include <vector> + +/* + * + * 9.41 + * + * + */ + +int main () { + + std::vector<char> 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; +} |