add Chapter06
This commit is contained in:
27
Chapter06/copy_if.cpp
Normal file
27
Chapter06/copy_if.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <vector>
|
||||||
|
#include <array>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
|
void print_container(const auto& container) {
|
||||||
|
|
||||||
|
for(auto& elem: container) {
|
||||||
|
printf("%d ", elem);
|
||||||
|
}
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::array<int, 10> src{0};
|
||||||
|
std::array<int, 10> 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;
|
||||||
|
}
|
||||||
17
Chapter06/function_overloading.cpp
Normal file
17
Chapter06/function_overloading.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
26
Chapter06/lvalue_refs.cpp
Normal file
26
Chapter06/lvalue_refs.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
BIN
Chapter06/main
Executable file
BIN
Chapter06/main
Executable file
Binary file not shown.
72
Chapter06/priority_queue_adaptor.cpp
Normal file
72
Chapter06/priority_queue_adaptor.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include <array>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <queue>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
template <typename T, size_t S> class fixed_vector : public std::array<T, S> {
|
||||||
|
|
||||||
|
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<T, S>::begin() + cnt_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const {
|
||||||
|
return cnt_ == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t cnt_ = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::priority_queue<int, fixed_vector<int, 10>> 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<int, fixed_vector<int, 10>> 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;
|
||||||
|
}
|
||||||
19
Chapter06/rvalue.cpp
Normal file
19
Chapter06/rvalue.cpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
27
Chapter06/rvalue_refs.cpp
Normal file
27
Chapter06/rvalue_refs.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
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<std::string> 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<std::string&&>(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;
|
||||||
|
}
|
||||||
34
Chapter06/sort.cpp
Normal file
34
Chapter06/sort.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <array>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
void print_container(const auto& container) {
|
||||||
|
|
||||||
|
for(auto& elem: container) {
|
||||||
|
printf("%d ", elem);
|
||||||
|
}
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::array<int, 10> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user