Last active
June 23, 2022 17:47
-
-
Save ochaochaocha3/11f15401667a14d041fcd90f16f412a9 to your computer and use it in GitHub Desktop.
Vi村さんの重複キー検索の問題(C++17)
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
map_key: map_key.cpp | |
$(CXX) -o $@ -std=gnu++17 -Wall -Wpedantic -g $^ |
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 <sstream> | |
#include <map> | |
#include <vector> | |
struct Hoge { | |
std::string hoge1; | |
std::string hoge2; | |
std::string hoge3; | |
}; | |
using StringHogeMap = std::map<std::string, Hoge>; | |
using StringStringHogeMap = std::map<std::string, StringHogeMap>; | |
// テスト用のmapを用意する | |
StringStringHogeMap setup_map(); | |
// mapの内容を出力する | |
void print_map(const StringStringHogeMap& m); | |
int main() { | |
StringStringHogeMap m = setup_map(); | |
std::cout << "[Map]" << std::endl; | |
print_map(m); | |
std::cout << std::endl; | |
using StringVector = std::vector<std::string>; | |
// 「key => keyを格納しているnameのvector」のmap | |
std::map<std::string, StringVector> key_names; | |
for (const auto& [name, key_value] : m) { | |
for (const auto& [key, _value] : key_value) { | |
auto it = key_names.find(key); | |
if (it == key_names.end()) { | |
// 初めて発見したkeyの場合、nameを格納するvectorを作る | |
key_names[key] = StringVector{}; | |
} | |
// vectorにnameを追加する | |
key_names[key].push_back(name); | |
} | |
} | |
std::cout << "[Duplication]" << std::endl; | |
for (const auto& [key, names] : key_names) { | |
if (names.size() > 1) { | |
// namesの要素が複数ある => 複数のnameに共通のkeyが存在する | |
std::cout << key << " < " << names[0]; | |
for (auto it = names.cbegin() + 1; it != names.cend(); ++it) { | |
std::cout << ", " << *it; | |
} | |
std::cout << std::endl; | |
} | |
} | |
} | |
// テスト用のmapを用意する | |
StringStringHogeMap setup_map() { | |
StringStringHogeMap m{}; | |
std::string name0{"name0"}; | |
m[name0] = StringHogeMap{}; | |
// "name0" に0-9の要素を挿入する | |
for (int i = 0; i < 10; ++i) { | |
std::stringstream ss_key; | |
ss_key << "key" << i; | |
std::string key = ss_key.str(); | |
std::stringstream ss_value; | |
ss_value << "hoge" << i; | |
std::string value = ss_value.str(); | |
m[name0][key] = Hoge{value, value, value}; | |
} | |
std::string name1{"name1"}; | |
// キーが重複している要素を挿入する | |
m[name1] = StringHogeMap{ | |
{std::string{"key0"}, {std::string{"hoge0"}, std::string{"hoge0"}, std::string{"hoge0"}}}, | |
{std::string{"key9"}, {std::string{"hoge9"}, std::string{"hoge9"}, std::string{"hoge9"}}}, | |
}; | |
// "name1" に10-19の要素を挿入する | |
for (int i = 10; i < 20; ++i) { | |
std::stringstream ss_key; | |
ss_key << "key" << i; | |
std::string key = ss_key.str(); | |
std::stringstream ss_value; | |
ss_value << "hoge" << i; | |
std::string value = ss_value.str(); | |
m[name1][key] = Hoge{value, value, value}; | |
} | |
return m; | |
} | |
// mapの内容を出力する | |
void print_map(const StringStringHogeMap& m) { | |
for (const auto& [name, key_value] : m) { | |
std::cout << name << ":" << std::endl; | |
for (const auto& [key, value] : key_value) { | |
std::cout << " " << key << ": {"; | |
std::cout << value.hoge1 << ", " << value.hoge2 << ", " << value.hoge3 << "}"; | |
std::cout << std::endl; | |
} | |
} | |
} |
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
[Map] | |
name0: | |
key0: {hoge0, hoge0, hoge0} | |
key1: {hoge1, hoge1, hoge1} | |
key2: {hoge2, hoge2, hoge2} | |
key3: {hoge3, hoge3, hoge3} | |
key4: {hoge4, hoge4, hoge4} | |
key5: {hoge5, hoge5, hoge5} | |
key6: {hoge6, hoge6, hoge6} | |
key7: {hoge7, hoge7, hoge7} | |
key8: {hoge8, hoge8, hoge8} | |
key9: {hoge9, hoge9, hoge9} | |
name1: | |
key0: {hoge0, hoge0, hoge0} | |
key10: {hoge10, hoge10, hoge10} | |
key11: {hoge11, hoge11, hoge11} | |
key12: {hoge12, hoge12, hoge12} | |
key13: {hoge13, hoge13, hoge13} | |
key14: {hoge14, hoge14, hoge14} | |
key15: {hoge15, hoge15, hoge15} | |
key16: {hoge16, hoge16, hoge16} | |
key17: {hoge17, hoge17, hoge17} | |
key18: {hoge18, hoge18, hoge18} | |
key19: {hoge19, hoge19, hoge19} | |
key9: {hoge9, hoge9, hoge9} | |
[Duplication] | |
key0 < name0, name1 | |
key9 < name0, name1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment