diff --git a/seminar04_templates/05_maximum/main.cpp b/seminar04_templates/05_maximum/main.cpp index 9df3256..37075cc 100644 --- a/seminar04_templates/05_maximum/main.cpp +++ b/seminar04_templates/05_maximum/main.cpp @@ -7,7 +7,7 @@ using std::string, std::pair, std::cout, std::endl, std::vector; template T maximum(const vector& v) { - T max; + T max{}; for(int i = 0, size = v.size(); i < size; ++i) { if (v[i] > max) max = v[i]; diff --git a/seminar05_iterators/01_slide/main.cpp b/seminar05_iterators/01_slide/main.cpp new file mode 100644 index 0000000..d5a23a7 --- /dev/null +++ b/seminar05_iterators/01_slide/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using std::cout, std::endl, std::vector, std::cin; + +int main() { + size_t n; + cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) + cin >> v[i]; + + vector::iterator max = std::max_element(v.begin(), v.end()); + std::sort(v.begin(), max); + std::sort(max, v.end()); + std::reverse(max, v.end()); + + for (int i = 0; i < v.size(); ++i) + cout << v[i] << (i == v.size() - 1 ? "": " "); + cout << endl; +} diff --git a/seminar05_iterators/02_string_vector_reverse/main.cpp b/seminar05_iterators/02_string_vector_reverse/main.cpp new file mode 100644 index 0000000..fb6f1b3 --- /dev/null +++ b/seminar05_iterators/02_string_vector_reverse/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +using std::cout, std::endl, std::string, std::vector; + +void string_vector_reverse(vector& sv) { + std::for_each(sv.begin(), sv.end(), [](string& s){ std::reverse(s.begin(), s.end()); }); + std::reverse(sv.begin(), sv.end()); +} + +int main() { + + vector sv1 {"cat", "dog", "mouse", "elephant"}; + vector sv2 {"a", "bc"}; + + string_vector_reverse(sv1); + string_vector_reverse(sv2); + + for (int i = 0; i < sv1.size(); ++i) + cout << sv1[i] << (i == sv1.size() - 1 ? "" : " "); + cout << endl; + for (int i = 0; i < sv2.size(); ++i) + cout << sv2[i] << (i == sv2.size() - 1 ? "" : " "); + cout << endl; +} diff --git a/seminar05_iterators/03_is_uppper/main.cpp b/seminar05_iterators/03_is_uppper/main.cpp new file mode 100644 index 0000000..a002373 --- /dev/null +++ b/seminar05_iterators/03_is_uppper/main.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include +#include + +using std::string, std::cin, std::cout, std::endl; + +bool strIsUpper(const string& s) { + return std::accumulate(s.begin(), s.end(), true, [](bool res, char c) {return res && (!isalpha(c) || isupper(c)); }); +} + +int main() { + string s; + getline(cin, s); + cout << strIsUpper(s) << endl; +} diff --git a/seminar05_iterators/04_is_identifier/main.cpp b/seminar05_iterators/04_is_identifier/main.cpp new file mode 100644 index 0000000..edcee1e --- /dev/null +++ b/seminar05_iterators/04_is_identifier/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +using std::cout, std::endl, std::string, std::string_view; + +bool isIdentifier(string_view sv) { + bool is = true; + if (!(std::isalpha(sv[0]) || sv[0] == '_')) + return false; + if (std::all_of(sv.begin(), sv.end(), [](const char c){ return isalpha(c) || isdigit(c) || c == '_' ;})) + return true; + return false; +} + +int main() { + string s; + getline(std::cin, s); + cout << isIdentifier(s) << endl; +} diff --git a/seminar05_iterators/05_move_spaces/main.cpp b/seminar05_iterators/05_move_spaces/main.cpp new file mode 100644 index 0000000..19dad49 --- /dev/null +++ b/seminar05_iterators/05_move_spaces/main.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +using std::cout, std::string, std::endl; + +void move_spaces(string& s) { + std::stable_sort(s.begin(), s.end(), [](const char& a, const char& b){ return (b == ' '); }); +} + +int main() { + string s; + std::getline(std::cin, s); + move_spaces(s); + // So we can see spaces + cout << s << "###" << endl; + +} diff --git a/seminar05_iterators/homework_iterators.pdf b/seminar05_iterators/homework_iterators.pdf new file mode 100644 index 0000000..86e4bdc Binary files /dev/null and b/seminar05_iterators/homework_iterators.pdf differ