Last active
October 31, 2019 14:09
-
-
Save JarvisCraft/d539515a38836a48750e9ed1b9711680 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 <string> | |
#include <vector> | |
#include <map> | |
#include <unordered_map> | |
#include <algorithm> | |
#ifndef in | |
#define in std::cin | |
#endif // in | |
#ifndef out | |
#define out std::cout | |
#endif // out | |
#ifndef nl | |
#define nl std::endl | |
#endif // nl | |
using std::string; | |
using std::back_inserter; | |
using std::count_if; | |
using std::transform; | |
using std::to_string; | |
using std::vector; | |
using std::pair; | |
using std::map; | |
using std::unordered_multimap; | |
template<typename K, typename V> | |
string to_string(const map<K, V> map) { | |
string value = "{"; | |
{ | |
const auto end = map.end(); | |
auto not_first = false; | |
for (auto iterator = map.begin(); iterator != end; iterator++) { | |
if (not_first) value += ", "; | |
else not_first = true; | |
const auto pair = *iterator; | |
value += (to_string(pair.first) + ":" + pair.second); | |
} | |
} | |
return value + "}"; | |
} | |
template<typename K, typename V> | |
string to_string(const unordered_multimap<K, V> map) { | |
string value = "{"; | |
{ | |
const auto end = map.end(); | |
auto not_first = false; | |
for (auto iterator = map.begin(); iterator != end; iterator++) { | |
if (not_first) value += ", "; | |
else not_first = true; | |
const auto pair = *iterator; | |
value += (to_string(pair.first) + ":" + pair.second); | |
} | |
} | |
return value + "}"; | |
} | |
int main() { | |
unordered_multimap<int, string> original_map; | |
/* Task 1 */ | |
{ | |
size_t size; | |
out << "Insert size: "; | |
in >> size; | |
for (size_t i = 0; i < size; i++) { | |
out << "Insert key and value: "; | |
int key; | |
string value; | |
in >> key >> value; | |
original_map.insert(pair<int, string>(key, value)); | |
} | |
out << "Read collection: " << to_string(original_map) << nl; | |
} | |
{ | |
original_map.erase(5); | |
original_map.erase(8); | |
out << "After erasing: " << to_string(original_map) << nl; | |
} | |
{ | |
for (auto i = 0U; i < 2U; i++) { | |
out << "Insert value for key 2: "; | |
string value; | |
in >> value; | |
original_map.insert(pair<int, string>(2, value)); | |
} | |
out << "After adding two values at 2: " << to_string(original_map) << nl; | |
} | |
map<int, string> shortened_map; | |
{ | |
const auto end = original_map.end(); | |
for (auto iterator = original_map.begin(); iterator != end; iterator++) { | |
const auto pair = *iterator; | |
shortened_map.insert(::pair<int, string> (pair.first, pair.second)); | |
} | |
out << "Shortened map: " << to_string(shortened_map) << nl; | |
} | |
/* Task 2 */ | |
{ | |
out << "Number of positive elements: " << count_if( | |
shortened_map.begin(), shortened_map.end(), | |
[](const pair<int, string> &pair) { return pair.first > 0; } | |
); | |
} | |
vector<int> keys; | |
transform( | |
shortened_map.begin(), shortened_map.end(), std::back_inserter(keys), | |
[](const pair<const int, const string>& pair) { return pair.first; } | |
); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment