From d1ceada8ed2b6897792aef7d3b2813d238262acd Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 10 Oct 2024 20:22:26 +0200 Subject: list --- reverse-containers.cpp | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'reverse-containers.cpp') diff --git a/reverse-containers.cpp b/reverse-containers.cpp index 4639887..fc39aed 100644 --- a/reverse-containers.cpp +++ b/reverse-containers.cpp @@ -111,6 +111,18 @@ std::string &reverse_string_no_tmp_iterators(std::string &sc) { return sc; } +std::list &reverse_list(std::list &lc) { + + auto beg = lc.begin(); + auto crbeg = lc.crbegin(); + for(decltype(lc.size()) i = 0 ; i != (lc.size() / 2) ; ++i, ++crbeg) { + lc.insert(beg, *crbeg); + } + + lc.erase(++beg, lc.cend()); + return lc; +} + void print_container(std::string &sc) { std::cout << sc << std::endl; @@ -131,6 +143,16 @@ void print_container(std::vector &vc) { std::cout << std::endl; } +void print_container(std::list &lc) { + + for(auto &b : lc) { + std::cout << b << " "; + } + + std::cout << std::endl; +} + + void print_container(std::deque &dc) { for(auto &b : dc) { @@ -158,7 +180,8 @@ int main(int argc, char **argv) { int seven = 0; int eight = 0; int nine = 0; - while ((opt = getopt(argc, argv, "123456789")) != -1) { + int zero = 0; + while ((opt = getopt(argc, argv, "1234567890")) != -1) { switch (opt) { case '1': @@ -204,17 +227,23 @@ int main(int argc, char **argv) { nine = 1; + break; + case '0': + + zero = 1; + break; } } - if((one + two + three + four + five + six + seven + eight + nine) > 1) { + if((one + two + three + four + five + six + seven + eight + nine + zero) > 1) { usageOUT(); return EXIT_FAILURE; } std::string input_str = "abcdef"; const char *input_cstr = "abcdef"; + std::list input_list = {'a', 'b', 'c', 'd', 'e', 'f'}; std::vector input_vec = {'a', 'b', 'c', 'd', 'e', 'f'}; std::deque input_deq = {'a', 'b', 'c', 'd', 'e', 'f'}; if(one == 1) { @@ -273,5 +302,11 @@ int main(int argc, char **argv) { std::cout << "reverse_string_no_tmp_iterators" << std::endl; } + if(zero == 1) { + reverse_list(input_list); + print_container(input_list); + std::cout << "reverse_list" << std::endl; + } + return 0; } -- cgit v1.2.3