Commit 39c7f19c authored by Šmelko Adam Mgr.'s avatar Šmelko Adam Mgr.
Browse files

add cv5

parent 9b4d5ef4
......@@ -27,6 +27,7 @@ Deadliny:
| 07.10.2021 | Stringy, znaky, streamy | [CV2](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv2.pdf) |
| 14.10.2021 | Hlavičky, třídy, objekty | [CV3](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv3.pdf) |
| 21.10.2021 | Kontejnery, iterátory | [CV4](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv4.pdf) |
| 04.11.2021 | Složitost operací, třídění | [CV5](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv5.pdf) |
## Úkoly:
......
#include <map>
#include <string>
#include <set>
#include <vector>
#include <iostream>
struct dictionary
{
private:
using translation_set = std::set<std::string>;
using dictionary_map = std::map<std::string, translation_set>;
dictionary_map map_;
public:
void add(const std::string& word, const std::string& translation)
{
map_[word].insert(translation);
}
void del(const std::string& word)
{
map_.erase(word);
}
void del(const std::string& word, const std::string& translation)
{
map_[word].erase(translation);
}
std::vector<std::string> get_tranlations(const std::string& word)
{
const auto& translations = map_[word];
return std::vector<std::string>(translations.begin(), translations.end());
}
struct iterator
{
private:
dictionary_map& map_;
dictionary_map::iterator dict_it_;
translation_set::iterator translation_it_;
public:
iterator(dictionary_map& map, dictionary_map::iterator initial_it)
: map_(map), dict_it_(initial_it)
{
if (dict_it_ != map_.end())
translation_it_ = dict_it_->second.begin();
}
std::pair<std::string, std::string> operator*()
{
return std::make_pair(dict_it_->first, *translation_it_);
}
bool operator!=(const iterator& oth) const
{
return &map_ != &oth.map_ || dict_it_ != oth.dict_it_ ||
(dict_it_ != map_.end() && translation_it_ != oth.translation_it_);
}
iterator& operator++()
{
translation_it_++;
if (translation_it_ == dict_it_->second.end())
{
dict_it_++;
if (dict_it_ != map_.end())
translation_it_ = dict_it_->second.begin();
}
return *this;
}
};
iterator begin()
{
return iterator(map_, map_.begin());
}
iterator end()
{
return iterator(map_, map_.end());
}
};
int main()
{
dictionary d;
d.add("a", "a");
d.add("a", "a1");
d.add("a", "a2");
d.add("a", "a3");
d.add("a", "a4");
d.add("b", "a");
d.add("b", "b");
d.add("c", "a");
for (auto [word, tran] : d)
{
std::cout << word << " " << tran << std::endl;
}
d.add("x", "x");
d.del("x");
std::cout << "program stale zije" << std::endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment