ЛКШ 2017.Август
ЛКШ 2017.Август
int x = 0b10010; // 18
const int EARTH_RADIUS = 40'000'000;
struct Date {
int year, month, day;
auto key() const { // tuple<const int&, ...>
return tie(year, month, day);
}
bool operator<(const Date& other) const {
return key() < other.key();
}
};
#include <functional>
set<int, greater<>> numbers;
greater
?#include <string>
cout << min("abc"s, "def"s) << endl;
"abc"s
имеет тип string
vector<int> numbers = {1, 2, 3};
auto x = numbers[1];
x
?vector<int> numbers = {1, 2, 3};
auto x = numbers[1];
x
?x
имеет тип int
, хотя numbers[1]
— int&
vector<int> numbers = {1, 2, 3};
decltype(auto) x = numbers[1];
x
имеет тип int&
auto avg = [](auto x, auto y) {
return (x + y) / 2;
};
cout << avg(5, 12) << " " <<
avg(5., 12.) << endl;
map<string, int> numbers = /* ... */;
for (const auto& [key, value] : numbers) {
cout << key << " " << value << endl;
}
auto [it, inserted] =
numbers.emplace("seventh", 7);
tuple t(5, "abc", false);
vector v = {1, 6, 3};
map<string, int> numbers;
if (auto it = numbers.find("one");
it != numbers.end()) {
cout << it->second;
}
#include <string_view>
string_view FindFirstWord(string_view s) {
return s.substr(0, s.find(' '));
}
#include <optional>
optional<string> MakeDigitName(uint8_t x) {
if (x == 0) {
return "zero";
} else if (x == 1) {
return "one";
} else {
return {};
}
}
#include <optional>
if (auto name = MakeDigitName(0)) {
cout << *name << endl;
} else {
cout << "Unknown digit" << endl;
}
#include <any>
any x = "abc"s;
cout << any_cast<string>(x) << endl;
x = 8;
cout << any_cast<int>(x) << endl;
#include <variant>
enum class Error { Negative, TooBig };
variant<string, Error> MakeDigitName(int x) {
if (x == 0) { return "zero"; }
else if (x < 0) { return Error::Negative; }
else { return Error::TooBig; }
}
if (auto res = MakeDigitName(12);
holds_alternative<string>(res)) {
cout << get<string>(res) << endl;
} else {
cout <<
static_cast<int>(get<Error>(res)) << endl;
}
#include <algorithm>
int n;
cin >> n;
cout << clamp(n, 0, 100) << endl;
cout << hypot(2, 3, 6) << endl; // 7
#include <numeric>
cout << gcd(6, 8) << endl; // 2
cout << lcm(6, 8) << endl; // 24
template <typename... Args>
auto Hypot(Args... args) {
return sqrt(((args * args) + ...));
}
template <typename T>
concept bool Summable =
requires(T x, T y) {
{x + y} -> T;
};
auto sum(Summable a, Summable b) {
return a + b;
}
template <typename T>
concept bool Summable =
requires(T x, T y) {
{x + y} -> T;
};
Summable{T}
T sum(T a, T b) { return a + b; }
vector<numbers> v = {1, 2, 3};
reverse(v);
vector<string> words = {"first", "second"};
// Сортировка по последней букве
sort(words, less<>,
[](const string& s) { return s.back(); })
vector<numbers> v = {1, 2, 3};
auto [min_it, max_it] = minmax_element(v);
cout << *min_it << " " << *max_it << endl;
auto res = minmax_element(v);
cout << *res.min() << " " <<
*res.max() << endl;
Я ленточка!