Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MOD = 61007;
- using namespace std;
- template<class KeyType, class ValueType> class IteratorHash {
- public:
- vector<std::list<pair<const KeyType, ValueType>>> *table_num;
- size_t key, ind;
- IteratorHash(vector<std::list<pair<const KeyType, ValueType>>> *table_num, size_t key, size_t ind) {
- this->table_num = table_num;
- this->key = key;
- this->ind = ind;
- }
- std::pair<const KeyType, ValueType> &operator*() {
- return *table_num[key][ind];
- }
- std::pair<const KeyType, ValueType> *operator->() {
- return &*table_num[key][ind];
- }
- IteratorHash &operator++() {
- ++ind;
- if (ind < *table_num[key].size()) {
- return *this;
- }
- ind = 0;
- ++key;
- return *this;
- }
- bool operator==(const IteratorHash &HashOther) const {
- return ((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
- }
- bool operator!=(const IteratorHash &HashOther) const {
- return !((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
- }
- IteratorHash operator++(int) {
- IteratorHash ans = *this;
- ++*this;
- return ans;
- }
- };
- template<class KeyType, class ValueType> class IteratorHashConst {
- public:
- vector<std::list<pair<const KeyType, ValueType>>> *table_num;
- size_t key, ind;
- IteratorHashConst(vector<std::list<pair<const KeyType, ValueType>>> *table_num, size_t key, size_t ind) {
- this->table_num = table_num;
- this->key = key;
- this->ind = ind;
- }
- const std::pair<const KeyType, ValueType> &operator*() const {
- return *table_num[key][ind];
- }
- const std::pair<const KeyType, ValueType> *operator->() const {
- return &*table_num[key][ind];
- }
- IteratorHashConst &operator++() {
- ++ind;
- if (ind < *table_num[key].size()) {
- return *this;
- }
- ind = 0;
- ++key;
- return *this;
- }
- bool operator==(const IteratorHashConst &HashOther) const {
- return ((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
- }
- bool operator!=(const IteratorHashConst &HashOther) const {
- return !((table_num == HashOther.table_num) && (key == HashOther.key) && (ind == HashOther.ind));
- }
- IteratorHashConst operator++(int) {
- IteratorHashConst ans = *this;
- ++*this;
- return ans;
- }
- };
- template<class KeyType, class ValueType, class Hash = std::hash<KeyType> > class HashMap {
- public:
- vector<vector<pair<KeyType, ValueType>>> table;
- typedef IteratorHash<const KeyType, ValueType> iterator;
- typedef IteratorHashConst<const KeyType, ValueType> const_iterator;
- Hash get_hash;
- size_t length;
- HashMap (Hash val = Hash()) {
- get_hash = val;
- table.resize(MOD);
- length = 0;
- }
- template <class IteratorOther> HashMap(IteratorOther left, IteratorOther right, Hash val = Hash()) {
- get_hash = val;
- table.resize(0);
- length = 0;
- while (left != right) {
- insert(*left++);
- }
- }
- HashMap(std::initializer_list<std::pair<KeyType, ValueType>> add, Hash val = Hash()) {
- get_hash = val;
- table.resize(0);
- length = 0;
- for (auto cur = add.begin(); cur != add.end(); ++cur) {
- insert(*cur);
- }
- }
- size_t size() const {
- return length;
- }
- bool empty() const {
- return (length == 0);
- }
- Hash hash_function() const {
- return get_hash;
- }
- void insert(const std::pair<KeyType, ValueType> &add) {
- size_t ind = get_hash(add.first) % MOD;
- if (!table[ind].empty()) {
- for (auto cur : table[ind]) {
- if (cur == add.first) {
- return;
- }
- }
- }
- table[ind].push_back(add);
- }
- void erase(const KeyType &to_del) {
- size_t ind = get_hash(to_del) % MOD;
- if (table[ind].empty()) {
- return;
- }
- for (size_t i = 0; i < table[ind].size(); ++i) {
- if (table[ind][i] == to_del) {
- for (size_t j = i; j < table[ind].size() - 1; ++i) {
- table[ind][j] = table[ind][j + 1];
- }
- table[ind].pop_back();
- --length;
- return;
- }
- }
- }
- iterator begin() {
- if (length == 0) {
- return this->end;
- }
- return iterator(table, 0, 0);
- }
- iterator end() {
- return iterator(table, table.size(), 0);
- }
- /*const_iterator begin() const {
- if (length == 0) {
- return this->end();
- }
- return const_iterator(table, 0, 0);
- }
- const_iterator end() const {
- return const_iterator(table, table.size(), 0);
- }*/
- iterator find(const KeyType &val) {
- size_t ind = get_hash(val) % MOD;
- if (table[val].empty) {
- return this->end();
- }
- for (size_t i = 0; i < table[ind].size(); ++i) {
- if (table[ind][i] == val) {
- return iterator(table, val, i);
- }
- }
- return this->end();
- }
- const_iterator find(const KeyType &val) const {
- size_t ind = get_hash(val) % MOD;
- if (table[val].empty) {
- return this->end();
- }
- for (size_t i = 0; i < table[ind].size(); ++i) {
- if (table[ind][i] == val) {
- return const_iterator(table, val, i);
- }
- }
- return this->end();
- }
- ValueType& operator[](const KeyType &val) {
- iterator cur = find(val);
- if (cur != this->end()) {
- return cur->second;
- }
- insert(std::make_pair(val, ValueType()));
- return find(val)->second;
- }
- const ValueType& at(const KeyType val) const {
- if (find(val) == end()) {
- throw std::out_of_range("");
- } else {
- return find(val)->second;
- }
- }
- void clear() {
- if (length == 0) {
- return;
- }
- length = 0;
- table.clear();
- table.resize(MOD);
- }
- };
- int main() {
- int n;
- cin >> n;
- vector<pair<int, string> > a(n);
- for (int i = 0; i < n; i++)
- cin >> a[i].first >> a[i].second;
- const HashMap<int, std::string> map(a.begin(), a.end());
- auto it = map.begin();
- ++it;
- auto other_it = it;
- //map.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement