静态 CPP 中的非静态成员
non static member in static cpp
下面的程序给出错误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?
声明自定义比较器类型,将其用作最终比较器参数的基础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
扩展中内联比较器的可能性更大。
不能访问静态成员函数中的非静态成员变量。此外,还应在类外部定义静态成员变量。下面的代码工作正常,没有任何编译警告和错误。
#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()
{
}
下面的程序错误地在静态中使用 mep 无效 功能
因为C++中的静态函数(comp
(不能访问非静态变量(mep
(
当我声明 mep 也为静态给出时,错误未定义 参考环保部
类的静态变量需要有一个定义,而不仅仅是声明。所以给mep
一个初始。
您可以将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
以解决此问题。
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何在C++中使用非静态成员函数作为回调函数
- (C++)为什么静态成员可以在初始化之前使用
- 类的全局对象和静态成员
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 模板化类中静态成员的延迟初始化
- 使用静态成员声明类时遇到问题
- C++:是否可以使用非静态成员变量模板?
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 静态 CPP 中的非静态成员
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 初始化 cpp 文件中的私有静态成员变量.错误:成员是私有的
- cpp-静态成员和函数
- 关于 cpp 的静态成员
- 如何通过 A 初始化 CPP 文件中的静态成员
- 使用.cpp文件中类的静态成员
- singleton--cpp文件中无法识别的静态成员
- 在.cpp中初始化私有静态成员时出现错误:未定义引用
- 在.cpp文件中定义需要访问私有结构的静态成员
- 是否可以在.cpp文件中定义类的静态成员函数而不是它的头文件?