小编use*_*886的帖子

防止覆盖和/或隐藏基类功能(C++ 11)

自从我学习C++以来,我甚至想要防止隐藏基类非虚函数一段时间,我不确定这是否符合道德规范,但C++ 11的功能给了我一个想法.假设我有以下内容:

bases.h ....

#ifndef baseexample_h
#define baseexample_h

#include <iostream>

class Base{
    public:  
    void foo() {
        std::cout << "Base.foo()\n" << std::endl;
    }
};

class Derived: public Base{ 
    public:
    void foo(){
        std::cout << "Derived.foo()\n" << std::endl;
    }   
};

#endif
Run Code Online (Sandbox Code Playgroud)

和main.cpp ...

#include "bases.h"
#include <iostream>

int main()
{

    Base base;
    Derived derived; 

    base.foo();
    derived.foo();

    std::cin.get();

    return 0;
};
Run Code Online (Sandbox Code Playgroud)

当然是输出

Base.foo()

Derived.foo()
Run Code Online (Sandbox Code Playgroud)

因为派生的foo()函数隐藏了基本的foo函数.我想防止可能的隐藏,所以我的想法是将头文件基本定义更改为:

//.....
class Base{
    public:  
    virtual void foo() final {
        std::cout << "Base.foo()\n" << std::endl;
    }
};

class …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance c++11

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

如何将静态成员添加到 Cython 类(来自 python,而不是 C)

我如何将静态类型成员添加到 cython 类?添加类型化实例-members 的语法使用如下语法(例如):

import cython

cdef class NoStaticMembers:

   cdef public int instanceValue # but, how do I create a static member??

   def __init__(self, int value):
       self.instanceValue = value
Run Code Online (Sandbox Code Playgroud)

c python static class cython

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

Matplotlib保存文件稍后将在ipython中重新编辑

有没有办法保存matplotlib图的"状态",以便我可以打开图并稍后与之交互,例如在新的ipython笔记本会话中.

如果您想稍后编辑图形,则绘制pdf和其他文件格式并不公平.

如果您想稍后对图形进行注释或重新缩放,这将非常有用,但您不一定能够访问生成图形的原始脚本/数据.

在matlab中,经常报告ipython试图以多种方式模拟,你可以简单地将文件保存为.fig甚至是脚本,如.m(matlab)文件!然后你重新打开你的.m.fig在稍后的matlab会话中编辑它.

这可以用matplotlib完成吗?

python matlab matplotlib

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

更简单的方法来过滤掉Python数组或numpy数组中的nan或invalid?

我有heights可能有nan它的numpy数组.我通过这样做来清理它:

heights = numpy.asarray([ h for h in heights if not numpy.isnan(h) ])
Run Code Online (Sandbox Code Playgroud)

这似乎是表达这种简单/常见事物的相当冗长的方式.我经常必须这样做以便以其他方式过滤我的阵列,并且必须回到阵列构建,这有效,但我敢打赌有更好的方法来做到这一点.例如按范围过滤......

heights = numpy.asarray(heights[lowerBound <= heights & heights < upperBound])
Run Code Online (Sandbox Code Playgroud)

在python中失败,高度仍然是一个numpy数组.我回到做......

编辑:此行的错误消息是:

TypeError:输入类型不支持ufunc'bitwise_and',根据强制转换规则''safe',输入无法安全地强制转换为任何支持的类型

/编辑

heights = numpy.asarray(heights[[h for h in heights if lowerBound <= h and h < upperBound]])
Run Code Online (Sandbox Code Playgroud)

毛.我现在已经使用python 2-3个月,但我仍然没有真正得到如何有效和简单地使用numpy masking系统.我来自大量使用matlab,其中"面具"将是一组相同形状/大小的布尔.例如

heights = heights(~isnan(heights));
Run Code Online (Sandbox Code Playgroud)

或者......

heights(isnan(heights)) = [];
Run Code Online (Sandbox Code Playgroud)

这两个看起来都很干净.此外,在python中失败的bounds示例在matlab中工作,尽管括号必须更改为括号...

heights = heights(lowerBound <= heights & heights < upperBound)
Run Code Online (Sandbox Code Playgroud)

我怎样才能在python/numpy,pythonic或其他方面优雅地编写这些简单的数据转换?

python arrays matlab numpy python-2.7

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

在MATLAB中为自定义类重载索引操作符`()`和`{}`

我希望能够在MATLAB中重载索引操作符 (){}我的类.特别是,我希望能够定义类方法,如...

% ...
classdef MyClass < handle

    % ... other class definition stuff...

    function returnVal = operator{}(indexRange)
        %....implementation here....
    end
    function returnVal = operator()(indexRange)
        %....implementation here....
    end
end
Run Code Online (Sandbox Code Playgroud)

这样我就可以创建对象并做...

x = MyClass();
returnedVals = x(:);
returnedCells = [x{:}];

% etc.
Run Code Online (Sandbox Code Playgroud)

这在MATLAB中是否可行?我知道这在C++和python中很容易(分别通过重载operator []__get__运算符).Mathworks网站本身并不太清楚如何做到这一点.

matlab operator-overloading operator-keyword

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

怀疑std :: array C++ 11库坏了吗?我该怎么办?

从谷歌我似乎无法找到明确的答案,但我不知道该怎么做.我怀疑在我用g ++的数组C++ 11库编写的一个更大的程序中可能存在问题,并编写了一个测试程序.我正在使用g ++ 4.8.1.

main.cpp中:

#include <iostream>
#include <array>
int main()
{
    std::array<int,5> vector;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的测试命令行编译是:

g++ main.cpp -std=c++11
Run Code Online (Sandbox Code Playgroud)

有错误..

In file included from main.cpp:2:0:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:107:12: error: 'array' has not been declared
       swap(array& __other)
            ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: In member function 'void std::Array<_Tp, _Nm>::swap(int&)':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:109:50: error: request for member 'begin' in '__other', which is of non-class type 'int'
       { std::swap_ranges(begin(), end(), __other.begin()); }
                                                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: At global scope:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:228:22: error: 'array' does not name a type
     operator==(const array<_Tp, _Nm>& …
Run Code Online (Sandbox Code Playgroud)

c++ arrays c++11

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