Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <unordered_map>
- void try_access_4(const std::unordered_map<int, char>& m) {
- // Оператор [] недоступен.
- std::cout << "Value at 4 = " << m.at(4) << std::endl;
- }
- int main1() {
- std::unordered_map<int, char> m;
- m.insert({5, 'f'});
- std::cout << "Count 4: " << m.count(4) << std::endl;
- std::cout << "Contains 5: " << (m.contains(5) ? "YES" : "NO") << std::endl;
- const auto i = m.find(5);
- std::cout << (i == m.end() ? "end" : "exists") << std::endl;
- std::cout << i->first << " " << i->second << std::endl;
- m.erase(5);
- std::cout << "Contains 5 after erase: " << (m.contains(5) ? "YES" : "NO") << std::endl;
- // Работа со значением.
- m.insert({5, 'g'});
- std::cout << "Value at 5 = " << m.at(5) << std::endl;
- m.at(5) = 'h';
- std::cout << "Value at 5 after = " << m.at(5) << std::endl;
- // at бросит исключение
- // m.at(4) = 'h';
- std::cout << "Value at 5 = " << m[5] << std::endl;
- try_access_4(m);
- std::cout << "Value at 4 = " << m[4] << std::endl;
- std::cout << "Is zero at 4? " << (m[4] == 0 ? "Zero" : "Non zero") << std::endl;
- return 0;
- }
- int main2() {
- int n = 0;
- std::cin >> n;
- std::unordered_map<std::string, int> s;
- for (int i = 0; i < n; ++i) {
- std::string str;
- std::cin >> str;
- // Вариант 1
- auto it = s.find(str);
- if (it != s.end()) {
- int value = ++it->second;
- if (value == 2) std::cout << str << std::endl;
- } else {
- s.insert({str, 1});
- }
- // Вариант 2
- // if (++s[str] == 2) std::cout << str << std::endl;
- }
- return 0;
- }
- int main3() {
- std::map<std::string, int> m;
- m.insert({"aba", 4});
- m.insert({"ac", 5});
- m.insert({"aaaaa", 6});
- auto& [a, b] = *(m.find("ac"));
- std::cout << a << " -> " << b << std::endl;
- for (auto& [key, value] : m) {
- std::cout << key << " -> " << value << std::endl;
- }
- return 0;
- }
- // Лесенка
- int main() {
- int n = 0;
- std::cin >> n;
- if (n <= 1) {
- std::cout << 1 << std::endl;
- return 0;
- }
- int s0 = 1;
- int s1 = 1;
- int s2 = 2;
- for (int i = 0; i < n - 2; ++i) {
- int result = s0 + s1 + s2;
- s0 = s1;
- s1 = s2;
- s2 = result;
- // ??? std::tie(s0, s1, s2) = std::tie(s1, s2, result);
- }
- std::cout << s2 << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement