{"id":14453,"date":"2024-03-15T09:11:38","date_gmt":"2024-03-15T09:11:38","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/"},"modified":"2025-08-06T08:54:00","modified_gmt":"2025-08-06T08:54:00","slug":"how-to-store-two-variables-in-an-unordered_set-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/","title":{"rendered":"Store Two Variables in C++ unordered_set"},"content":{"rendered":"<p>An unordered_set in C++ is a container that stores unique elements in an unordered manner. It can store elements of any custom type, including cases where it saves two variables.<\/p>\n<p>To store two variables, you can use a custom struct or class as the element type in an unordered_set. Here is an example using a custom struct:<\/p>\n<pre class=\"post-pre\"><code class=\"lang-cpp\">#include &lt;iostream&gt;\r\n#include &lt;unordered_set&gt;\r\n\r\nstruct MyPair {\r\n    int x;\r\n    int y;\r\n};\r\n\r\nstruct MyPairHash {\r\n    size_t operator()(const MyPair&amp; pair) const {\r\n        return std::hash&lt;int&gt;()(pair.x) ^ std::hash&lt;int&gt;()(pair.y);\r\n    }\r\n};\r\n\r\nstruct MyPairEqual {\r\n    bool operator()(const MyPair&amp; lhs, const MyPair&amp; rhs) const {\r\n        return lhs.x == rhs.x &amp;&amp; lhs.y == rhs.y;\r\n    }\r\n};\r\n\r\nint main() {\r\n    std::unordered_set&lt;MyPair, MyPairHash, MyPairEqual&gt; mySet;\r\n\r\n    mySet.insert({1, 2});\r\n    mySet.insert({3, 4});\r\n    mySet.insert({1, 2}); \/\/ \u91cd\u590d\u7684\u5143\u7d20\u4e0d\u4f1a\u88ab\u63d2\u5165\r\n\r\n    for (const auto&amp; pair : mySet) {\r\n        std::cout &lt;&lt; pair.x &lt;&lt; \", \" &lt;&lt; pair.y &lt;&lt; std::endl;\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<p>In the example above, MyPair is a custom struct that includes two integer variables x and y. MyPairHash is a custom hash function used to calculate the hash value of MyPair type. MyPairEqual is a custom equality comparison function used to determine if elements of MyPair type are equal.<\/p>\n<p>In the main function, an unordered_set object called mySet is created and several elements of the type MyPair are inserted using the insert function. Note that duplicate elements will not be inserted because the elements in the unordered_set are unique.<\/p>\n<p>Finally, iterate through mySet with a for loop and output the values of the member variables x and y for each MyPair element.<\/p>\n<p>You can customize the member variables, hash function, and equal comparison function of the MyPair struct according to your own requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An unordered_set in C++ is a container that stores unique elements in an unordered manner. It can store elements of any custom type, including cases where it saves two variables. To store two variables, you can use a custom struct or class as the element type in an unordered_set. Here is an example using a [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[7690,19450,19452,19451,13401],"class_list":["post-14453","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-stl","tag-c-unordered_set","tag-container-optimization","tag-custom-struct","tag-hash-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Store Two Variables in C++ unordered_set - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Store Two Variables in C++ unordered_set\" \/>\n<meta property=\"og:description\" content=\"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:11:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T08:54:00+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"Store Two Variables in C++ unordered_set\",\"datePublished\":\"2024-03-15T09:11:38+00:00\",\"dateModified\":\"2025-08-06T08:54:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\"},\"wordCount\":201,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"C++ STL\",\"C++ unordered_set\",\"container optimization\",\"custom struct\",\"hash function\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\",\"name\":\"Store Two Variables in C++ unordered_set - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:11:38+00:00\",\"dateModified\":\"2025-08-06T08:54:00+00:00\",\"description\":\"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Store Two Variables in C++ unordered_set\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Store Two Variables in C++ unordered_set - Blog - Silicon Cloud","description":"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Store Two Variables in C++ unordered_set","og_description":"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:11:38+00:00","article_modified_time":"2025-08-06T08:54:00+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"Store Two Variables in C++ unordered_set","datePublished":"2024-03-15T09:11:38+00:00","dateModified":"2025-08-06T08:54:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/"},"wordCount":201,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["C++ STL","C++ unordered_set","container optimization","custom struct","hash function"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/","name":"Store Two Variables in C++ unordered_set - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:11:38+00:00","dateModified":"2025-08-06T08:54:00+00:00","description":"Learn how to store two variables in C++ unordered_set using custom structs with hash functions. Includes code examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-store-two-variables-in-an-unordered_set-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Store Two Variables in C++ unordered_set"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14453"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14453\/revisions"}],"predecessor-version":[{"id":158482,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14453\/revisions\/158482"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}