<i id='bRkjr'><tr id='bRkjr'><dt id='bRkjr'><q id='bRkjr'><span id='bRkjr'><b id='bRkjr'><form id='bRkjr'><ins id='bRkjr'></ins><ul id='bRkjr'></ul><sub id='bRkjr'></sub></form><legend id='bRkjr'></legend><bdo id='bRkjr'><pre id='bRkjr'><center id='bRkjr'></center></pre></bdo></b><th id='bRkjr'></th></span></q></dt></tr></i><div id='bRkjr'><tfoot id='bRkjr'></tfoot><dl id='bRkjr'><fieldset id='bRkjr'></fieldset></dl></div>

  • <tfoot id='bRkjr'></tfoot>

    <small id='bRkjr'></small><noframes id='bRkjr'>

        <legend id='bRkjr'><style id='bRkjr'><dir id='bRkjr'><q id='bRkjr'></q></dir></style></legend>
          <bdo id='bRkjr'></bdo><ul id='bRkjr'></ul>

        C++ unordered_set 向量

        时间:2023-09-16
          <legend id='AnbwP'><style id='AnbwP'><dir id='AnbwP'><q id='AnbwP'></q></dir></style></legend>
          <i id='AnbwP'><tr id='AnbwP'><dt id='AnbwP'><q id='AnbwP'><span id='AnbwP'><b id='AnbwP'><form id='AnbwP'><ins id='AnbwP'></ins><ul id='AnbwP'></ul><sub id='AnbwP'></sub></form><legend id='AnbwP'></legend><bdo id='AnbwP'><pre id='AnbwP'><center id='AnbwP'></center></pre></bdo></b><th id='AnbwP'></th></span></q></dt></tr></i><div id='AnbwP'><tfoot id='AnbwP'></tfoot><dl id='AnbwP'><fieldset id='AnbwP'></fieldset></dl></div>

                <bdo id='AnbwP'></bdo><ul id='AnbwP'></ul>
                  <tbody id='AnbwP'></tbody>

                <tfoot id='AnbwP'></tfoot>
              • <small id='AnbwP'></small><noframes id='AnbwP'>

                  本文介绍了C++ unordered_set 向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我可以在 C++ 中创建一个 unordered_set 向量吗?像这样

                  Can I create a unordered_set of vectors in C++? something like this

                  std::unordered_set<std::vector<int>> s1;
                  

                  因为我知道使用 std lib 的set"类是可能的,但似乎它不适用于无序版本谢谢

                  because I know that is possible with the "set" class of the std lib but seems that it doesn't work for the unordered version thanks

                  更新:这正是我要使用的代码

                  Update: this is the exactly code that I'm trying to use

                  typedef int CustomerId;
                  typedef std::vector<CustomerId> Route;
                  typedef std::unordered_set<Route> Plan;
                  
                  // ... in the main
                  Route r1 = { 4, 5, 2, 10 };
                  Route r2 = { 1, 3, 8 , 6 };
                  Route r3 = { 9, 7 };
                  Plan p = { r1, r2 };
                  

                  如果我使用set就可以了,但是在尝试使用无序版本时我收到一个编译错误

                  and it's all right if I use set, but I receive a compilation error when try to use the unordered version

                  main.cpp:46:11: error: non-aggregate type 'Route' (aka 'vector<CustomerId>') cannot be initialized with an initializer list
                      Route r3 = { 9, 7 };
                  

                  推荐答案

                  当然可以.不过,您必须提出一个哈希值,因为默认值 (std::hash>) 将不会实现.例如,基于这个答案,我们可以构建:

                  Sure you can. You'll have to come up with a hash though, since the default one (std::hash<std::vector<int>>) will not be implemented. For example, based on this answer, we can build:

                  struct VectorHash {
                      size_t operator()(const std::vector<int>& v) const {
                          std::hash<int> hasher;
                          size_t seed = 0;
                          for (int i : v) {
                              seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
                          }
                          return seed;
                      }
                  };
                  

                  然后:

                  using MySet = std::unordered_set<std::vector<int>, VectorHash>;
                  

                  如果您愿意,您也可以为这种类型的 std::hash 添加专门化(注意可以em> 是 std::vector 的未定义行为,但对于用户定义的类型绝对没问题):

                  You could also, if you so choose, instead add a specialization to std::hash<T> for this type (note this could be undefined behavior with std::vector<int>, but is definitely okay with a user-defined type):

                  namespace std {
                      template <>
                      struct hash<std::vector<int>> {
                          size_t operator()(const vector<int>& v) const {
                              // same thing
                          }
                      };
                  }
                  
                  using MySet = std::unordered_set<std::vector<int>>;
                  

                  这篇关于C++ unordered_set 向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <i id='Ilv9r'><tr id='Ilv9r'><dt id='Ilv9r'><q id='Ilv9r'><span id='Ilv9r'><b id='Ilv9r'><form id='Ilv9r'><ins id='Ilv9r'></ins><ul id='Ilv9r'></ul><sub id='Ilv9r'></sub></form><legend id='Ilv9r'></legend><bdo id='Ilv9r'><pre id='Ilv9r'><center id='Ilv9r'></center></pre></bdo></b><th id='Ilv9r'></th></span></q></dt></tr></i><div id='Ilv9r'><tfoot id='Ilv9r'></tfoot><dl id='Ilv9r'><fieldset id='Ilv9r'></fieldset></dl></div>
                  <legend id='Ilv9r'><style id='Ilv9r'><dir id='Ilv9r'><q id='Ilv9r'></q></dir></style></legend>

                    <tfoot id='Ilv9r'></tfoot>
                        <tbody id='Ilv9r'></tbody>

                          <bdo id='Ilv9r'></bdo><ul id='Ilv9r'></ul>

                          <small id='Ilv9r'></small><noframes id='Ilv9r'>