【问题标题】:non static member in static cpp静态 cpp 中的非静态成员
【发布时间】:2019-12-30 09:18:55
【问题描述】:

下面的程序给出错误invalid use of mep in static function

当我将 mep 也声明为静态给出错误 undefined reference to mep

当我声明 comp 为非静态且 mep 为非静态时 我给出错误invalid use of non static member in sort

我应该怎么做才能在 leetcode 中提交这个解决方案类?

class Solution {
public:
     unordered_map<char,int>mep;
     static bool comp(string a,string b){
        int n = min(a.size(),b.size());
        for(int i=0;i<n;i++){
            int diff = mep[a[i]]-mep[b[i]];
            if(diff<0)return false;
            if(diff>0)return true;
        }
        return true;
    }
    bool isAlienSorted(vector<string>& words, string order) {
        for(int i=0;i<order.size();i++){
            mep[order[i]]=i;
        }
        vector<string>temp;
        temp=words;
        sort(temp.begin(),temp.end(),comp);
        return temp==words;

    }
};

我知道比较器的其他方法可以是 lambda 函数,哪一个是有效的上述或 lambda?

【问题讨论】:

  • mep 是类的非静态成员,因此只能在类的静态成员中用作实例的成员(例如diff = some_solution.mep[a[i]] - some_solution.mep[b[i]]) - 比如comp() .
  • 您在排序时需要mep作为临时助手吗?

标签: c++ sorting comparator


【解决方案1】:

声明一个自定义比较器type,将其用作std::sort 的最终比较器参数的基础。在此过程中,您获得了可重用性; static 实现非常缺乏的东西。

class Solution {
public:

    struct Comp
    {
        unordered_map<char, int> mep;

        bool operator()(std::string const& a, std::string const& b)
        {
            size_t n = min(a.size(), b.size());
            for (size_t i = 0; i<n; i++) {
                int diff = mep[a[i]] - mep[b[i]];
                if (diff<0)
                    return false;
                if (diff>0)
                    return true;
            }
            return true;
        }
    };

    bool isAlienSorted(vector<string> const& words, string order)
    {
        Comp comp;
        for (int i = 0; i<order.size(); i++) {
            comp.mep[order[i]] = i;
        }

        vector<string>temp = words;
        sort(temp.begin(), temp.end(), comp);
        return temp == words;
    }
};

关于您的最后一个问题,编译为优化代码并使用可靠的基准进行测量(不像听起来那么容易)。如果甚至有明显的差异,我的钱都花在了 lambda 上(这几乎就是我们上面所拥有的),因为编译器更有可能将比较器内联到 std::sort 扩展中。

【讨论】:

    【解决方案2】:

    您不能在静态成员函数中访问非静态成员变量。此外,您还应该在类之外定义静态成员变量。下面的代码工作正常,没有任何编译警告和错误。

    #include <string>
    #include <iostream>
    #include <unordered_map>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    class Solution {
    public:
       static unordered_map<char, int> mep;
       static bool comp(string a, string b) {
          int n = min(a.size(), b.size());
          for (int i = 0; i < n; i++) {
             int diff = mep[a[i]] - mep[b[i]];
             if (diff < 0)return false;
             if (diff > 0)return true;
          }
          return true;
       }
       bool isAlienSorted(vector<string>& words, string order) {
          for (size_t i = 0; i < order.size(); i++) {
             mep[order[i]] = i;
          }
          vector<string> temp;
          temp = words;
          sort(temp.begin(), temp.end(), comp);
          return temp == words;
    
       }
    };
    
    unordered_map<char, int> Solution::mep;
    
    
    void main()
    {
    }
    

    【讨论】:

      【解决方案3】:

      下面的程序在静态中给出错误无效使用 mep 功能

      因为 C++ 中的静态函数 (comp) 无法访问非静态变量 (mep)

      当我将 mep 也声明为静态时,给出错误未定义 参考mep

      一个类的静态变量需要有一个定义而不仅仅是声明。所以给mep一个初始化。

      【讨论】:

        【解决方案4】:

        您可以将 unordered_map 作为参数传递给 comp 函数。这样您就不会访问非静态对象,但需要您编写自己的排序算法:

        static bool comp(const unordered_map<char, int>& map_mep, string a, string b)
         {
              int n = min(a.size(), b.size());
              for (int i = 0; i < n; i++) {
                 // this way there is no non-static involved
                 int diff = map_mep[a[i]] - map_mep[b[i]];
                 if (diff < 0)return false;
                 if (diff > 0)return true;
              }
              return true;
           }
        

        因为mep 不是类的静态成员,所以静态函数看不到它。您应该将 mep 设置为 static 以解决此问题。

        【讨论】:

        • comp 函数由std::sort 调用,因此缺少关于如何将mep 传递给comp 的重要部分。
        • 我的错,我错过了。我调整了答案。
        • 我仍然看不到任何关于将mep 传递给comp 应该如何在问题中显示的代码中工作的解释。
        • 我添加了对实现外观的解释。
        • 您仍然没有解释如何将mep 传递给comp。代码中comp的用法是sort(temp.begin(),temp.end(),comp);,所以compstd::sort调用,但std::sort不知道mepbool comp(const unordered_map&lt;char, int&gt;&amp; map_mep, string a, string b)也不会是一个有效的签名std::sort的第三个参数。
        猜你喜欢
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        • 1970-01-01
        • 2012-10-20
        • 2016-05-20
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多