From 0d66d06314fff368a1aed7e3600eec14017454b6 Mon Sep 17 00:00:00 2001 From: pezy_mbp Date: Mon, 15 Dec 2014 23:42:35 +0800 Subject: [PATCH] refactor 11.11 and 11.12_13 --- ch11/README.md | 4 +++ ch11/ex11_11.cpp | 48 ++++++++++----------------- ch11/ex11_12_13.cpp | 80 ++++++++++++++++----------------------------- 3 files changed, 49 insertions(+), 83 deletions(-) diff --git a/ch11/README.md b/ch11/README.md index 92863fe2..a0c11cb2 100644 --- a/ch11/README.md +++ b/ch11/README.md @@ -33,3 +33,7 @@ I use `set` when i just need to store the `key`, In other hand, I would like u ## [Exercise 11.7](ex11_7.cpp) ## [Exercise 11.8](ex11_8.cpp) +## [Exercise 11.9 and 11.10](ex11_9_10.cpp) +## [Exercise 11.11](ex11_11.cpp) +## [Exercise 11.12 and 11.13](ex11_12_13.cpp) +## [Exercise 11.14](ex11_14.cpp) diff --git a/ch11/ex11_11.cpp b/ch11/ex11_11.cpp index 0760d1c5..9d66dc39 100644 --- a/ch11/ex11_11.cpp +++ b/ch11/ex11_11.cpp @@ -1,37 +1,23 @@ -//! @Alan -//! Exercise 11.11: -//! Redefine bookstore without using decltype. -// discussion on stack overflow: -// http://stackoverflow.com/questions/20608365/is-it-possible-to-code-this-waywhilelambda -//! -#include -#include -#include -#include -#include +// +// ex11_11.cpp +// Exercise 11.11 +// +// Created by pezy on 12/15/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Redefine bookstore without using decltype. + +#include "../ch07/ex7_26.h" #include - - -class A +bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) { - int lenth; -public: - int getLenth() const {return lenth;} -}; - -bool compareA(const A &a1, const A &a2) -{ - return a1.getLenth() < a2.getLenth(); + return lhs.isbn() < rhs.isbn(); } + int main() { - //! more approaches can be found on the post of SO. - bool (*fp) (const A &a1, const A &a2) = compareA; - - std::multiset m1(fp); - - return 0; -} - - + using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs); + //typedef bool(*compareType)(const Sales_data &lhs, const Sales_data &rhs); + std::multiset bookstore(compareIsbn); +} \ No newline at end of file diff --git a/ch11/ex11_12_13.cpp b/ch11/ex11_12_13.cpp index 3d50acf6..f8c2bd06 100644 --- a/ch11/ex11_12_13.cpp +++ b/ch11/ex11_12_13.cpp @@ -1,57 +1,33 @@ -//! @Alan -//! Exercise 11.12: -//! Write a program to read a sequence of strings and ints, storing -//! each into a pair. Store the pairs in a vector. -//! -//! Exercise 11.13: -//! There are at least three ways to create the pairs in the program -//! for the previous exercise. Write three versions of that program, -//! creating the pairs in each way. Explain which form you think is -//! easiest to write and understand, and why. -//! -#include -#include -#include -#include -#include -#include +// +// ex11_12_13.cpp +// Exercise 11.12 11.13 +// +// Created by pezy on 12/15/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Write a program to read a sequence of strings and ints, +// storing each into a pair. Store the pairs in a vector. +// +// There are at least three ways to create the pairs in the program for the previous exercise. +// Write three versions of that program, creating the pairs in each way. +// Explain which form you think is easiest to write and understand, and why. + +#include #include - +#include +#include int main() { + std::vector> vec; + std::string str; int i; - std::string word; - - std::pair pair; - std::vector> v; - - while([&]() - { - std::cout << "enter a word:\n"; - std::cin >> word; - std::cout << "enter an int:\n"; - std::cin >> i; - - return word != "@q"; -}()) - { - //! way 1: - //pair = {word, i}; - //!^^^^^^^^^^^^^^^^^^^ - //! way one is the easiest to write and understand. - //! way 2: - //pair = std::make_pair(word,i); - //! way 3: - pair = std::pair(word,i); - - v.push_back(pair); - for(auto e : v) - std::cout << e.first << " " - << e.second << " "; - std::cout << "\n"; - } - return 0; -} - - + while (std::cin >> str >> i) + vec.push_back(std::pair(str, i)); + //vec.push_back(std::make_pair(str, i)); + //vec.push_back({str, i}); + //vec.emplace_back(str, i); //!!! easiest way. + + for (const auto &p : vec) + std::cout << p.first << ":" << p.second << std::endl; +} \ No newline at end of file