自从我学习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) 我如何将静态类型成员添加到 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) 有没有办法保存matplotlib图的"状态",以便我可以打开图并稍后与之交互,例如在新的ipython笔记本会话中.
如果您想稍后编辑图形,则绘制pdf和其他文件格式并不公平.
如果您想稍后对图形进行注释或重新缩放,这将非常有用,但您不一定能够访问生成图形的原始脚本/数据.
在matlab中,经常报告ipython试图以多种方式模拟,你可以简单地将文件保存为.fig
甚至是脚本,如.m
(matlab)文件!然后你重新打开你的.m
或.fig
在稍后的matlab会话中编辑它.
这可以用matplotlib完成吗?
我有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或其他方面优雅地编写这些简单的数据转换?
我希望能够在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网站本身并不太清楚如何做到这一点.
从谷歌我似乎无法找到明确的答案,但我不知道该怎么做.我怀疑在我用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) matlab ×3
python ×3
arrays ×2
c++ ×2
c++11 ×2
c ×1
class ×1
cython ×1
inheritance ×1
matplotlib ×1
numpy ×1
python-2.7 ×1
static ×1