diff options
author | Oskar <[email protected]> | 2024-08-22 19:21:59 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-22 19:21:59 +0200 |
commit | 456ce2efc1a2f14cdaa853ec9a95945a77ff2a08 (patch) | |
tree | 0b3ac22690d6aeeab1dfe76124d032c184ea13c1 | |
parent | 4c8377e5711e2c864c8754d1d6373c09da2ce7e1 (diff) |
more
-rw-r--r-- | 4p31.cpp | 26 | ||||
-rw-r--r-- | 4p32.cpp | 23 | ||||
-rw-r--r-- | 4p33.cpp | 23 |
3 files changed, 72 insertions, 0 deletions
diff --git a/4p31.cpp b/4p31.cpp new file mode 100644 index 0000000..07bc64f --- /dev/null +++ b/4p31.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <vector> + +/* + * + * 4.31 + * + * There is no difference, because what is returned is discarded. + * ++ix and --cnt is technically better because we return nothing + * but there isnt a difference because both programs do the same thing. + */ + +int main () { + + std::vector<int> ivec(10); + std::vector<int>::size_type cnt = ivec.size(); + for(std::vector<int>::size_type ix = 0 ; ix != ivec.size() ; ix++, cnt--) { + ivec[ix] = cnt; + } + + for(auto a : ivec) { + std::cout << a << std::endl; + } + + return 0; +} diff --git a/4p32.cpp b/4p32.cpp new file mode 100644 index 0000000..346b82e --- /dev/null +++ b/4p32.cpp @@ -0,0 +1,23 @@ +#include <iostream> + +/* + * + * 4.32 + * + * We have a pointer thats basically like a begin() iterator + * We have ix that starts at 0 + * We keep looping as long as ix is not equal to the size + * We keep looping as long as ptr does not point to one past the last element in ia + * This ensures that we can safely do stuff to the array inside the loop + */ + +int main () { + + constexpr int size = 5; + int ia[size] = {1,2,3,4,5}; + for (int *ptr = ia, ix = 0 ; ix != size && ptr != ia+size ; ++ix, ++ptr){ + /* . . . */ + } + + return 0; +} diff --git a/4p33.cpp b/4p33.cpp new file mode 100644 index 0000000..fa4f199 --- /dev/null +++ b/4p33.cpp @@ -0,0 +1,23 @@ +#include <iostream> + +/* + * + * 4.33 + * + * + */ + +int main () { + + bool someValue = true; + int res; + int x = 1; + int y = 1; + // (sameValue ? (++x, ++y) : --x), --y + res = someValue ? ++x, ++y : --x, --y; + std::cout << res << " res"<< std::endl; + std::cout << x << " x" << std::endl; + std::cout << y << " y" << std::endl; + + return 0; +} |