Last active
March 22, 2021 14:28
-
-
Save yeputons/007fd5cc39c96e7db2131913e75bad4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
void make_good() { | |
} | |
// #define vector<int> vi | |
using vi = std::vector<int>; | |
// #define vector v | |
template<typename T> | |
using v = std::vector<T>; | |
// v<int> --> std::vector<int> | |
// #define N 100000 | |
constexpr int N = 100'000; | |
int main() { | |
int *a = malloc(sizeof(int)) * 10; // C | |
int *b = new int[10]; // C++, bad style | |
std::vector<int> vec(10); | |
int arr[10]; // C++, bad style | |
std::sort(arr, arr + 10); // C++, bad style | |
std::sort(arr, arr + std::size(arr)); | |
std::sort(std::begin(arr), std::end(arr)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <vector> | |
#include <iostream> | |
// 1, 2, 3, 4, 2, 3, 10, 5, -10, 6 | |
// 1, 1, 1, 1, 1, 1, 1, 1, -10, -10 | |
// template<typename T> | |
struct min_stack { | |
private: | |
std::vector<int> data; | |
std::vector<int> cur_min; | |
public: | |
void push_back(int val) { | |
data.emplace_back(val); | |
if (cur_min.empty()) { | |
cur_min.emplace_back(val); | |
} else { | |
cur_min.emplace_back(std::min(val, cur_min.back())); | |
} | |
} | |
int top() { | |
return data.back(); | |
} | |
int get_min() { | |
assert(!data.empty()); | |
return cur_min.back(); | |
} | |
void pop_back() { | |
assert(!data.empty()); | |
data.pop_back(); | |
cur_min.pop_back(); | |
} | |
}; | |
int main() { | |
min_stack s; | |
s.push_back(10); | |
s.data.push_back(10); | |
//std::cout << s.data.back() << "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <set> | |
struct Summator { | |
int secret = 0; | |
// operator+, operator+=, operator<: берём и перегружаем, но осторожно. | |
// operator=: правило трёх, правило пяти | |
int operator() (int a, int b, int c) { | |
return a + b + c + secret; | |
} | |
}; | |
struct Comparator { | |
int center; | |
bool operator()(int a, int b) const { | |
return std::abs(a - center) < std::abs(b - center); | |
} | |
}; | |
int main() { | |
// Summator s; | |
// s.secret = 10; | |
// std::cout << s(1, 2, 3) << "\n"; | |
//Comparator c; | |
//std::vector v{1, 2, 3, 4, 5}; | |
//std::sort(std::begin(v), std::end(v), c); | |
Comparator c; | |
c.center = 3; | |
std::set<int, Comparator> s({1, 2, 3, 4, 5}, c /* копируется */); | |
c.center = -100; // Ни на что не влияет. | |
s.insert(10); | |
s.insert(-1); | |
for (int x : s) | |
std::cout << x << "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://cppinsights.io/s/9aaaa0d2 | |
int main() | |
{ | |
auto f = [](int a, int b) { | |
return a + b; | |
}; | |
int secret = 10; | |
int secret2 = 20; | |
// Так можно захватить все переменны: = (по значению, копируем) или & (по ссылке). | |
auto g = [secret, &secret2, secret3 = secret + secret2](int a, int b) { | |
secret2++; // Меняет внешнюю переменную: её не копировали. | |
return a + b + secret + secret2 + secret3; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <functional> | |
template<typename FnSuperCoolType> | |
void repeat_n(int n, FnSuperCoolType f) { | |
for (int i = 0; i < n; i++) { | |
f(); | |
} | |
} | |
int main() { | |
int x = 0; | |
repeat_n(10, [&x]() { | |
x++; | |
}); // Дёшево: все типы известны на этапе компиляции. | |
std::cout << x << "\n"; | |
std::vector<std::function<void()>> vec{ | |
[]() { std::cout << "1\n"; }, | |
[]() { std::cout << "2\n"; } | |
}; | |
vec[0](); // Дорого! Не знаем тип на этапе компиляции. | |
vec[1](); | |
vec[0](); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <chrono> | |
#include <functional> | |
#include <iostream> | |
#include <set> | |
template <typename Fn> void timeit(int n, Fn fn) { | |
auto start = std::chrono::steady_clock::now(); | |
for (int i = 0; i < n; i++) { | |
fn(); | |
} | |
auto duration = std::chrono::steady_clock::now() - start; | |
std::cout | |
<< "Took " | |
<< std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() | |
<< "ms\n"; | |
} | |
int main() { | |
int a = 0; | |
std::function<void()> f = [&a]() { a++; }; | |
auto g = [&a]() { a++; }; | |
const int N = 100'000'000; | |
timeit(N, f); | |
assert(a == N); | |
timeit(N, g); | |
assert(a == 2 * N); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment