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

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

        c++设置容器的问题

        problems with c++ set container(c++设置容器的问题)
        <tfoot id='81oRn'></tfoot>

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

            • <legend id='81oRn'><style id='81oRn'><dir id='81oRn'><q id='81oRn'></q></dir></style></legend>
                <bdo id='81oRn'></bdo><ul id='81oRn'></ul>

                • <small id='81oRn'></small><noframes id='81oRn'>

                  本文介绍了c++设置容器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试编译以下代码时:

                  When I try to compile the following code:

                      #include <iostream>
                      #include <set>
                      #include <vector>
                  
                      using namespace std;
                  
                      template <class T, class S> 
                      class Property
                      {
                      public:
                          pair<T,S> p;
                  
                          Property(T t, S s) { p = make_pair(t,s);}
                  
                      };
                  
                      int main()
                      {
                      set< Property<string, string> > properties;
                      Property<string, string> name("name", "Andy");
                  
                      properties.insert(name);
                  
                      }
                  

                  我收到编译错误.但是,当我用向量替换 set 并因此使用 push_back 函数而不是插入函数时,一切正常.谁能解释一下我做错了什么?谢谢指教.

                  I get the compilation error. However, when I replace set by vector and hence use the the push_back function instead of insert function everything works fine. Could anyone explain me what am I doing wrong? Thanks in advice.

                  推荐答案

                  std::set 将其值存储在已排序的二叉树中,因此它需要知道如何比较它所保存的值.默认情况下,它使用 std::less作为比较函数,对于非专业化的用户定义类型,它尝试调用 operator<.因此,告诉集合如何比较您的对象的最简单方法是为您的类定义一个 operator<:

                  std::set stores its values in a sorted binary tree, so it needs to know how to compare the values it holds. By default it uses std::less as a comparison function, which for un-specialized user defined types tries to call operator<. So, the easiest way to tell the set how to compare your objects is to define an operator< for your class:

                  template <class T, class S> 
                  class Property
                  {
                  public:
                      pair<T,S> p;
                  
                      Property(T t, S s) { p = make_pair(t,s);}
                  
                      bool operator<(const Property<T,S>& rhs) const
                      {
                          return p < rhs.p;
                      }
                  };
                  

                  然而,还有其他方法可以告诉 std::set 如何比较您的类型.一种是为您的类专门化 std::less 模板:

                  However, there are also other ways of telling std::set how to compare your type. One is to specialize the std::less template for your class:

                  namespace std {
                  template<typename T,typename S>
                  struct less<Property<T, S> >
                  {
                      bool operator()(const Property<T, S>& lhs, const Property<T,S>& rhs) const
                      {
                          return lhs.p < rhs.p;
                      }
                  };
                  }
                  

                  另一种方法是用具有正确签名的函数或具有使用正确签名定义的 operator() 的类替换默认比较类型.这就是事情开始变得丑陋的地方.

                  Another is to replace the default comparison type with a function with the correct signature, or a class that has an operator() defined with the correct signature. This is where things start to get ugly.

                  // Comparison function
                  template<typename T, typename S>
                  bool property_less_function(const Property<T,S>& lhs, const Property<T,S>& rhs)
                  {
                      return lhs.p < rhs.p;
                  }
                  
                  // Comparison functor
                  template<typename T, typename S>
                  struct PropertyLess
                  {
                      bool operator()(const Property<T,S>& lhs, const Property<T,S>& rhs) const
                      {
                          return lhs.p < rhs.p;
                      }
                  };
                  
                  int main()
                  {
                      // Set using comparison function. 
                      // Have to pass the function pointer in the constructor so it knows
                      // which function to call. The syntax could be cleaned up with some
                      // typedefs.
                      std::set<Property<std::string, std::string>, 
                          bool(*)(const Property<std::string, std::string>&, 
                                  const Property<std::string, std::string>&)> 
                              set1(&property_less_function<std::string, std::string>);
                  
                      // Set using comparison functor. Don't have to pass a value for the functor
                      // because it will be default constructed.
                      std::set<Property<std::string, std::string>, PropertyLess<std::string, std::string> > set2;
                  }
                  

                  请记住,无论您使用什么小于函数,该函数都必须定义一个严格弱排序 适合您的类型.

                  Keep in mind that whatever less-than function you use, that function must define a strict weak ordering for your type.

                  这篇关于c++设置容器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                    <bdo id='xf6M7'></bdo><ul id='xf6M7'></ul>

                  • <small id='xf6M7'></small><noframes id='xf6M7'>

                        <legend id='xf6M7'><style id='xf6M7'><dir id='xf6M7'><q id='xf6M7'></q></dir></style></legend>

                          <tbody id='xf6M7'></tbody>

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