From c6319be7315706f9e0900fbf5895c18cc9044388 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Thu, 4 Sep 2014 12:05:54 +1200 Subject: [PATCH] Create ex3_21_generics_version To replace ex3_21.cpp This one is generics version using template, for general version please check @pezy's code. --- ch03/ex3_21_generics_version | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ch03/ex3_21_generics_version diff --git a/ch03/ex3_21_generics_version b/ch03/ex3_21_generics_version new file mode 100644 index 00000000..aac3307e --- /dev/null +++ b/ch03/ex3_21_generics_version @@ -0,0 +1,44 @@ +//! +//! @author Yue Wang +//! @date 4th Sep 2014 +//! + +#include +#include + +/** + * @brief check + * @param first + * @param last + * + * generics version for ex3.21 + */ +template +void check(Iter first, Iter last) +{ + for(auto it = first; it != last; ++it) + std::cout << *it << "\n"; + + std::cout << "size = " << last - first << "\n\n"; +} + +int main() +{ + std::vector v1; + std::vector v2(10); + std::vector v3(10, 42); + std::vector v4{10}; + std::vector v5{10, 42}; + std::vector v6{10}; + std::vector v7{10, "hi"}; + + check(v1.begin(),v1.end()); + check(v2.begin(),v2.end()); + check(v3.begin(),v3.end()); + check(v4.begin(),v4.end()); + check(v5.begin(),v5.end()); + check(v6.begin(),v6.end()); + check(v7.begin(),v7.end()); + + return 0; +}