小编dea*_*ock的帖子

为什么 'int' 在 C++ 中只用作 int 而为什么不用作 std::int ?

standard当前 C++ 中的所有内容不同,是否有任何特定原因要离开int, char, ....main()和其他人。(不是说+, -, %,.. 因为它们不是特定于语言的)

为什么不像:

std::int std::main(std::int argc, std::char *argv[])
{
    //Sample C++ code (incorrect with current standards though)
    std::return 0;
}
Run Code Online (Sandbox Code Playgroud)

超出标准范围的标准化不是不完整的吗?

我相信,它们是编写程序时随处可见的基本组件,无论是简单的还是复杂的。它们不包含在标准化中,只是为了遵循DRY原则。

c++ standards std

8
推荐指数
2
解决办法
604
查看次数

C 关键字/函数是否未包含在 C++ 的 std 命名空间中?

以下代码在我的本地系统上运行良好,但在在线平台上引发了编译错误。

#include <iostream>

int32_t time[int32_t(1e5)];

int main()
{
    int32_t n;
    std::cin>>n;
    for(int32_t i=0;i<n;++i)
    {
        int32_t temp;
        std::cin>>temp;
        --temp;
        ++time[temp];
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译错误描述

显然,错误是由于第 3 行int32_t time[int32_t(1e5)]; 而产生的但是,我无法理解为什么会发生编译错误。

我没有包含ctime或任何这样的标题,甚至没有解开std命名空间。但是仍然发生了编译失败。

我的系统上有带有 __cplusplus 201703 的 gcc 8.3.0。不确定在线平台上的版本是什么。

c++ std backwards-compatibility

8
推荐指数
2
解决办法
237
查看次数

第 924 行:字符 9:运行时错误:引用绑定到类型为“int”的空指针 (stl_vector.h)

在 leetcode 上运行以下代码时出现运行时错误。当我删除用户定义的比较器功能时,它工作正常。但是使用用户定义的比较器函数,它会给出如下运行时错误:

第 924 行:字符 9:运行时错误:引用绑定到类型为“int”(stl_vector.h)的空指针总结:UndefinedBehaviorSanitizer:undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8 /../../../../include/c++/8/bits/stl_vector.h:933:9

class Solution {
private:
    static bool comp (vector<int> p1, vector<int> p2) {
        if(p1[0] < p2[0] || p1[1] < p2[1])
            return true;
        else
            return false;
    }
    
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        if (envelopes.empty())
            return 0;
        sort(envelopes.begin(), envelopes.end(), comp);
        vector<int> dp(envelopes.size(), 1);
        int res = 0;
        
        for (int i = 0; i < envelopes.size(); i++) {
            for (int j = i-1; j >=0; j--) {
                if (envelopes[j][0] < envelopes[i][0] && envelopes[j][1] < envelopes[i][1] …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
8390
查看次数

在循环条件中,i&lt;sqrtN(预先计算)或 i*i&lt;N,在 C++ 中哪个更有效?

假设对于给定的整数 N,我需要运行 N 次的循环平方根。

在 C++ 中,我可以通过这两种方式来做到这一点——

1)

long long sqrtN = std::sqrt(N);
for (long long i=1; i < sqrtN; i++) {
    // some piece of code
}
Run Code Online (Sandbox Code Playgroud)
for (long long i=1; i * i < N; i++) {
    // some same piece of code
}
Run Code Online (Sandbox Code Playgroud)

我发现 std::sqrt() 具有 O(logn) 复杂度。另外,我相信数字的乘法只是一个常数时间运算。

所以,感觉第2个版本更快。但是,对于非常大的 n 值,第二个版本中的恒定时间可能很重要。

因此,我不确定哪种方式更有效?

c++ loops conditional-statements

1
推荐指数
1
解决办法
97
查看次数

如何绘制 Pandas Dataframe 中每列的空值计数

我有一个数据框(df),如下所示

    Column1 Column2 ...
0      1       1    
1    Null      1
.
.
Run Code Online (Sandbox Code Playgroud)

我想绘制每列中空值的计数

目前,我正在做

    Column1 Column2 ...
0      1       1    
1    Null      1
.
.
Run Code Online (Sandbox Code Playgroud)

每列中的空值计数

问题是大约有 180 列,其中大多数有 0 个空值,我想在绘图时忽略这些列。

我尝试了以下似乎不起作用

    df.isnull().sum().plot.bar()
    plt.show()
Run Code Online (Sandbox Code Playgroud)

新剧情

python dataframe pandas

1
推荐指数
2
解决办法
1万
查看次数