diff --git a/Lab2/README.md b/Lab2/README.md new file mode 100644 index 0000000..257425a --- /dev/null +++ b/Lab2/README.md @@ -0,0 +1,32 @@ +# CCIoT Homework Lab 2 + +## Slide 12 + +### Problem + +Try the following: + +1. Create a vector of whatever type you like (minimum length of 10); +2. Print it’s content; +3. Erase the odd elements; +4. Replace the first element and the last element with another value; +5. Create a rule that erases the 5th element from the vector; +6. Use cppreference in the meantime ☺ + +### Solution + +See [slide12.cpp](./slide12.cpp). + +## Slide 13 + +### Problem + +Try the same stuff with std::list, observe the differences. + +### Solution + +See [slide13.cpp](./slide13.cpp). + +The difference observed is at line 38. +Because `std::list` has a bidirectional iterator, not a random access one, skipping 4 elements cannot be done. +Instead, I had to use `std::next` in order to advance the iterator 4 times separately in order to find the 5th element and erase it. diff --git a/Lab2/slide12.cpp b/Lab2/slide12.cpp new file mode 100644 index 0000000..ca733e4 --- /dev/null +++ b/Lab2/slide12.cpp @@ -0,0 +1,47 @@ +#include +#include + +// 2. +template +void printEnumerable(const T &enumerable) { + bool first = true; + for (const auto& item : enumerable) { + if (first) first = false; + else std::cout << ", "; + + std::cout << item; + } + std::cout << std::endl; +} + +// 4. +template +void replaceFirstLast(T& enumerable, const V& newValue) { + *std::begin(enumerable) = newValue; + *std::rbegin(enumerable) = newValue; +} + +// 5. +template +void erase5th(T& enumerable) { + enumerable.erase(std::begin(enumerable) + 4); +} + +int main() { + // 1. + std::vector v {8, 4, 6, 1, 5, 3, 9, 5, 8, 123, 1, 2, 31, 24, 124, 12, 42, 131}; + + // 2. + printEnumerable(v); + + // 4. + replaceFirstLast(v, 7); + + // 5. + erase5th(v); + + // Print again to see changes + printEnumerable(v); + + return 0; +} \ No newline at end of file diff --git a/Lab2/slide13.cpp b/Lab2/slide13.cpp new file mode 100644 index 0000000..8d41391 --- /dev/null +++ b/Lab2/slide13.cpp @@ -0,0 +1,47 @@ +#include +#include + +// 2. +template +void printEnumerable(const T &enumerable) { + bool first = true; + for (const auto& item : enumerable) { + if (first) first = false; + else std::cout << ", "; + + std::cout << item; + } + std::cout << std::endl; +} + +// 4. +template +void replaceFirstLast(T& enumerable, const V& newValue) { + *std::begin(enumerable) = newValue; + *std::rbegin(enumerable) = newValue; +} + +// 5. +template +void erase5th(T& enumerable) { + enumerable.erase(std::next(std::begin(enumerable), 4)); +} + +int main() { + // 1. + std::list v {8, 4, 6, 1, 5, 3, 9, 5, 8, 123, 1, 2, 31, 24, 124, 12, 42, 131}; + + // 2. + printEnumerable(v); + + // 4. + replaceFirstLast(v, 7); + + // 5. + erase5th(v); + + // Print again to see changes + printEnumerable(v); + + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 73aefe4..5e7ae03 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # CCIoT Homework - [Lab1](./Lab1/) +- [Lab2](./Lab2/)