据我所知,在c ++中没有包含迭代器和reverse_iterator的公共基类.
到目前为止我唯一看到的建议是使用模板来解决这个问题( 如何编写一个以通用方式获取迭代器或集合的函数?)
然而,这个解决方案似乎对我不起作用.
class MyClass
{
template<typename Iter> Iter* generate_iterator(...params...)
{
//returns either a vector::iterator or vector::reverse_iterator
}
template<typename Iter> void do_stuff(Iter *begin, Iter *end)
{
//does stuff between elements specified by begin and end
//I would like this function to remain agnostic of which direction it is working in!
}
void caller()
{
//I would like this function to remain agnostic of which direction it is working in too...
do_stuff(generate_iterator(blah),generate_iterator(foo));
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,generate_iterator()不能按需使用,因为编译器抱怨"generate_iterator不是MyClass类的成员",大概是因为我没有指定它(我实际上不能这样做,因为调用者应该不知道迭代器类型).
有人可以帮忙吗?提前致谢!
编辑:正如Mark …
我有自己的数组类模板,我想可选择添加功能.
作为功能的一个例子,采用多线程支持:在某些情况下,我需要放在#pragma omp atomic任何更新代码之前的数组(强制执行原子行为的编译器指令,细节并不重要).在其他情况下,我需要不这样做的数组,因为我知道它们只会安全更新,我需要避免性能损失.
直觉上应该可以定义一个AtomicUpdates我可以继承的类.因此,为了定义一个带有原子更新的双数组,我会说类似的东西
class AtomicDoubleArray : public MyArray<double>, public AtomicUpdates {};
Run Code Online (Sandbox Code Playgroud)
但是我看不出你在实践中如何实现它,这也会破坏继承接口的原则,而不是实现.
任何人都可以告诉我我真正想做的事情吗?
我想知道有谁可以解释autolisp/visual lisp的以下功能背后的设计理由?对我而言,他们似乎面对公认的软件实践......我错过了什么?
/在函数参数中放置)10表示x/y坐标,90表示坐标列表的长度,63表示颜色等.好的,您可以将它们存储在某些常量中,但这意味着更多的全局变量,文档鼓励您直接使用幻数.如何Toplevel()在tkinter中生成子窗口,当父关闭时不关闭?
我是否需要让父母保留子窗口的"引用计数",拦截WM_DELETE_WINDOW并且只root.destroy()在所有孩子都离开时打电话?
或者用自己的tk 生成另一个线程进程是否可以接受mainloop?
还是有更优雅的方式?
编辑
我目前正在这样做
root = Tk()
app = App(root) # doesn't call Toplevel()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
在没有调用的情况下App.__init__()添加小部件,并在某些时候使用此函数生成一个新窗口:rootToplevel()
def new_window():
root = Tk()
window = App2(root) # doesn't call Toplevel() either
Run Code Online (Sandbox Code Playgroud)
请注意,rootin new_window()是与原始变量不同的变量root,通过另一次调用获得Tk().
所有这些似乎都是正确的事情,即子窗口独立于父窗口而生成,并且python进程在两者关闭后都会死亡.
所以我的问题变成了,这是否有意义,或者我在这里做了一些可怕的错误?
看起来很奇怪,即使在设置之后restype,python 也会返回long而不是c_void_p.
例如;
# python code
from ctypes import *
dll = windll.LoadLibrary("my.dll")
dll.my_object_create.restype = c_void_p
x = dll.my_object_create()
print type(x) # prints <type 'long'>
//c++ code
my_object *my_object_create() { return new my_object(); }
void my_object_destroy(my_object *obj) { delete obj; }
Run Code Online (Sandbox Code Playgroud)
我最近不得不修复一个错误,x反馈到另一个ctypes函数,指针被踩踏.通过将初始dll调用更改为已修复此问题
x = c_void_p(dll.my_object_create())
Run Code Online (Sandbox Code Playgroud)
...我猜测沿线ctypes处理x为4字节长而不是8(64位架构).
所以我想知道现有的行为是否导致你进入这个陷阱?
我的程序结构如下,这是一个巨大的CPU占用.整个系统的IO速度慢,我几乎无法移动鼠标指针......
...为什么?我以为THREAD_MODE_BACKGROUND_BEGIN应该阻止这个?
#pragma omp parallel
{
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
#pragma omp for
for (...)
{
doTruckLoadsOfComputation();
if (omp_get_thread_num()==0)
doTinyAmountOfIO(); //progress indicator
}
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
}
Run Code Online (Sandbox Code Playgroud)
更新:
添加SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);修复CPU占用问题,但问题仍然存在,为什么单独的后台模式不够?
处理计算量大的任务的常用 VB 方法是将其放入后台工作线程,而主线程继续处理 UI。
不管出于什么原因,我需要以相反的方式执行此操作:主线程执行繁重的工作,后台线程更新 UI。
这是我到目前为止所拥有的。唯一的问题是,虽然 UI 窗口 (Form1) 确实被重绘,但您无法与其交互,甚至无法移动它或调整其大小(鼠标光标变成沙漏状并且无法单击)。
Public Class ProgressDisplay
Private trd As Thread
Public Sub New()
trd = New Thread(AddressOf threadtask)
trd.Start()
End Sub
Private Sub threadtask()
Dim f1 As Form1
f1 = New Form1
f1.Show()
Do
f1.Update()
Thread.Sleep(100)
Loop
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
编辑:理想情况下,我需要向客户端呈现这样的界面
Public Class ProgressDisplay
Public Sub New()
Public Sub Update(byval progress as int)
End Class
Run Code Online (Sandbox Code Playgroud)
客户端将像这样调用它(实际上是在 COM 上的非托管 c++ 中,但你明白了):
Dim prog = new ProgressDisplay()
DoLotsOfWork(addressof prog.update) ' DoLotsOfWork method takes …Run Code Online (Sandbox Code Playgroud) vb.net multithreading message-pump ui-thread applicationcontext
我想创建一个非静态方法,只有类的相同实例(或其子类之一)的构造函数可以调用.除了面向密钥的访问保护模式之外,还有一种优雅的方法吗?
class MyClass
{
public:
void foo()
{
assert(foo was called from the constructor); //how?!
if (some condition or other)
throw ExceptionThatOnlyClientsThatConstructTheObjectCanHandle(); //hence my requirement
}
};
class MySubClass : public MyClass
{
public:
MySubClass()
{
blah(); //correct use of foo() through blah()
foo(); //correct use of foo() directly
}
void blah() { foo(); } //correctness depends on who called blah()
};
int main()
{
MySubClass m;
m.foo(); // incorrect use of foo()
m.blah(); // incorrect use of foo() through …Run Code Online (Sandbox Code Playgroud) 通过COM接收和发送数组的正确方法是什么?到目前为止,这是我的尝试:包含在变体中的双重安全阵列.
//takes variant holding safearray of doubles
//returns a similar variant having multipled every element by 2
STDMETHODIMP MyComClass::safearraytimestwo(VARIANT in, VARIANT* out)
{
CComSafeArray<double> sa_in;
sa_in.Attach(*in.pparray);
ULONG size = sa_in.GetCount();
CComSafeArray<double> *out_sa = new CComSafeArray<double>(size);
for (long i=0;i<size;i++)
out_sa->SetAt(i,sa_in[i]*2);
out = new CComVariant(out_sa);
return S_OK;
}
Run Code Online (Sandbox Code Playgroud)
问题: - 当前编译在循环操作上失败:error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ATL::_ATL_AutomationType<DOUBLE>::_typewrapper' (or there is no acceptable conversion) 编辑:解决使用SetAt()而不是operator[]
- 我应该out_sa在堆上声明吗?在取消分配时它会out …
假设我希望使用线性插值将时间序列重新索引到预定义索引,其中旧索引和新索引之间不共享任何索引值。例如
# index is all precise timestamps e.g. 2018-10-08 05:23:07
series = pandas.Series(data,index)
# I want rounded date-times
desired_index = pandas.date_range("2010-10-08",periods=10,freq="30min")
Run Code Online (Sandbox Code Playgroud)
教程/API 建议的方法是reindex使用interpolate. 但是,由于新旧索引之间的日期时间没有重叠,因此 reindex 输出所有 NaN:
# The following outputs all NaN as no date times match old to new index
series.reindex(desired_index)
Run Code Online (Sandbox Code Playgroud)
我不想在此期间填充最近的值,reindex因为这会失去精度,所以我想出了以下内容;在插值之前将重新索引的系列与原始系列连接起来:
pandas.concat([series,series.reindex(desired_index)]).sort_index().interpolate(method="linear")
Run Code Online (Sandbox Code Playgroud)
这看起来效率很低,将两个系列串联然后排序。有没有更好的办法?