diff --git a/Chapter06/copy_if.cpp b/Chapter06/copy_if.cpp new file mode 100644 index 0000000..f04406e --- /dev/null +++ b/Chapter06/copy_if.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include + +void print_container(const auto& container) { + + for(auto& elem: container) { + printf("%d ", elem); + } + printf("\r\n"); +} + +int main() { + + std::array src{0}; + std::array dst{0}; + + std::iota(src.begin(), src.end(), 0); + std::copy_if(src.begin(), src.end(), dst.begin(),[] (int x) {return x > 3;}); + + print_container(src); + print_container(dst); + + return 0; +} diff --git a/Chapter06/function_overloading.cpp b/Chapter06/function_overloading.cpp new file mode 100644 index 0000000..42cc8e6 --- /dev/null +++ b/Chapter06/function_overloading.cpp @@ -0,0 +1,17 @@ +#include + +void print(int a) { + printf("Int %d\r\n", a); +} + +void print(float a) { + printf("Float %2.f\r\n", a); +} + +int main() { + + print(2); + print(2.f); + + return 0; +} diff --git a/Chapter06/lvalue_refs.cpp b/Chapter06/lvalue_refs.cpp new file mode 100644 index 0000000..5389734 --- /dev/null +++ b/Chapter06/lvalue_refs.cpp @@ -0,0 +1,26 @@ +#include + + + +int main() { + + int a = 42; + + int& a_ref = a; + + const int& a_const_ref = a; + + + printf("a = %d\r\n", a); + + + a_ref = 16; + + printf("a = %d\r\n", a); + + + // a_const_ref = 16; compiler error + + return 0; + +} diff --git a/Chapter06/main b/Chapter06/main new file mode 100755 index 0000000..e3401c7 Binary files /dev/null and b/Chapter06/main differ diff --git a/Chapter06/priority_queue_adaptor.cpp b/Chapter06/priority_queue_adaptor.cpp new file mode 100644 index 0000000..6c23739 --- /dev/null +++ b/Chapter06/priority_queue_adaptor.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +template class fixed_vector : public std::array { + + public: + void push_back(const T &el) { + if(cnt_ < S) { + this->at(cnt_) = el; + ++cnt_; + } + } + + T &back() { + return this->at(cnt_-1); + } + + void pop_back() { + if(cnt_) { + --cnt_; + } + } + + auto end() { + return std::array::begin() + cnt_; + } + + bool empty() const { + return cnt_ == 0; + } + + private: + size_t cnt_ = 0; +}; + +int main() { + + std::priority_queue> pq; + + pq.push(10); + pq.push(4); + pq.push(8); + pq.push(1); + pq.push(2); + + printf("Popping elements from priority queue: "); + + while(!pq.empty()) { + printf("%d ", pq.top()); + pq.pop(); + } + + std::stack> st; + + st.push(10); + st.push(4); + st.push(8); + st.push(1); + st.push(2); + + printf("\r\nPopping elements from stack (LIFO): "); + + while(!st.empty()) { + printf("%d ", st.top()); + st.pop(); + } + printf("\r\n"); + + return 0; +} diff --git a/Chapter06/rvalue.cpp b/Chapter06/rvalue.cpp new file mode 100644 index 0000000..7c047a9 --- /dev/null +++ b/Chapter06/rvalue.cpp @@ -0,0 +1,19 @@ +#include + +struct my_struct { + + int a_; + + my_struct() : a_(0) {} + + my_struct(int a) : a_(a) {} + +}; + +int main() { + + printf("a_ = %d\r\n", my_struct().a_); + printf("a_ = %d\r\n", (my_struct()=my_struct(16)).a_); + + return 0; +} diff --git a/Chapter06/rvalue_refs.cpp b/Chapter06/rvalue_refs.cpp new file mode 100644 index 0000000..90f9fef --- /dev/null +++ b/Chapter06/rvalue_refs.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + + std::string str = "Hello world, this is move semantics demo!!!"; + printf("str.data address is %p\r\n", (void*)str.data()); + + std::vector v; + + v.push_back(str); + printf("str after copy is <%s>\r\n", str.data()); + + v.push_back(std::move(str)); + //v.push_back(static_cast(str)); + + printf("str after move is <%s>\r\n", str.data()); + + for(const auto & s:v) { + printf("s is <%s>\r\n", s.data()); + printf("s.data address is %p\r\n", (void*)s.data()); + } + + return 0; +} diff --git a/Chapter06/sort.cpp b/Chapter06/sort.cpp new file mode 100644 index 0000000..da2b2f5 --- /dev/null +++ b/Chapter06/sort.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +void print_container(const auto& container) { + + for(auto& elem: container) { + printf("%d ", elem); + } + printf("\r\n"); +} + +int main() { + + std::array src{0}; + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distrib(1, 6); + + auto rand = [&](int x) -> int { + return distrib(gen); + }; + + std::transform(src.begin(), src.end(), src.begin(), rand); + print_container(src); + + + std::sort(src.begin(), src.end()); + print_container(src); + + return 0; +}