#include <iostream>
#include "site_code_pl.h"
#include <map>
#include <unordered_map>
#include <chrono>
#include <random>
#include <limits>
#include <boost/functional/hash.hpp>
std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<std::size_t> dist(0, std::numeric_limits<std::size_t>::max());
static std::size_t g_seed = dist(gen);
struct dictionary {
std::multimap<std::string, std::wstring> mmap;
auto const get_dict() const { return mmap; }
};
template <class type>
struct cmp_key {
bool operator() (type const& lhs, type const& rhs) const
{
return lhs == rhs;
}
};
template <class type>
struct my_hash {
std::size_t operator() (type const& val) const
{
std::size_t seed = g_seed; // g_seed losowany jest na początku programu i jest niezmieniany w trakcie
boost::hash_combine(seed, val);
return seed;
}
};
struct unordered_dictionary {
std::unordered_multimap<
std::string, std::wstring, my_hash<std::string>, cmp_key<std::string>> unmmap;
void init_unord_mmap(std::multimap<std::string, std::wstring> const& dict)
{
for (auto const& item : dict)
{
unmmap.insert(decltype(unmmap)::value_type(item.first, item.second));
}
}
auto const get_unord_dict() const { return unmmap; }
};
template <class coll_t>
void print_map(coll_t const& coll)
{
for (auto const& item : coll) {
std::cout << item.first << " - ";
std::wcout << item.second << L"\n";
}
}
int main()
{
pl();
dictionary dict;
dict.mmap.insert({
{ "meadow" , L"łąka" },
{ "fear" , L"lęk" },
{"fear", L"strach"},
{ "rose" , L"róża" },
{ "knife" , L"noż" },
{ "spoon" , L"łyżka" }
});
print_map(dict.get_dict());
std::cout << "\n\n";
unordered_dictionary unord_dict;
unord_dict.init_unord_mmap(dict.get_dict());
print_map(unord_dict.get_unord_dict());
}
Niestety ten program generuje mi za każdym razem wyniki w tej samej kolejności.
Nie mogę zrozumieć jak to działa.
Gdy zamienię
std::size_t seed = g_seed;
na
std::size_t seed = dist(gen);
wtedy program generuje wyniki za każdym razem w różnej kolejności,
ale chyba jest to UB wg standardu (23.2.5.5)
edit: Przepraszam za brak pytania.
Czy mogę użyć drugiego zapisu, ew co mogę zrobić by mieć przy każdym uruchomieniu programu elementy ułożone w różnej kolejności?