小编mat*_*ots的帖子

使用 Python 诅咒的回调

我有如下代码:

stdscr.nodelay(1)
while True:
    c = stdscr.getch()
    if c != -1:
        stdscr.addstr("%s was pressed\n" % c)
    if time() - last > 1:
        stdscr.addstr("time() == %s\n" % str(time()))
        last = time()
Run Code Online (Sandbox Code Playgroud)

但是,我担心我的代码真的很浪费/效率低下。是否有可用于诅咒的回调机制?如果没有,处理这种情况的规范方法是什么?

python curses callback

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

在Java中复制java.util.Stack

我正在寻找一种java.util.Stack在Java中复制的正确方法.

Stack似乎没有复制构造函数,我唯一能够在文档中找到可能做我想要的东西的clone方法就是方法.

但是,如果我写的话

Stack<Integer> stack = (Stack<Integer>) oldstack.clone();
Run Code Online (Sandbox Code Playgroud)

编译器给我一个关于未选中类型的警告.

有没有"适当的"方式让我复制Stack?如果没有,这里最明智的做法是什么?我宁愿不要有任何警告,我觉得有点愚蠢的迭代和复制价值(虽然后者可能会成为最明智的事情).

java stack copy

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

在emacs regexp中使用"Mx发生"的"或"运算符

我想要概述一下我的Python程序,所以我跑了:

M-x occur

而且我提供的正则表达式

(def)|(class)

没能匹配任何东西.

我也看过这篇文章,试过了

(def)\\|(class)

但这无法匹配任何东西......

我如何M-x occur匹配classdef

regex emacs

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

C++向量有效地添加元素

假设我有以下代码:

#include <iostream>
#include <vector>
using namespace std;

class X {
public:
  int x[1000];
  X(int y) { for (int i = 0; i < 1000; i++) x[i] = y; }
};

int main() {
  vector<X> v;
  X x0(0);
  X x1(1);
  X x2(2);
  v.push_back(x0);
  v.push_back(x1);
  v.push_back(x2);
  cout << v[2].x[33] << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我理解正确的话,在我的代码,我栈上分配内存x0,x1并且x2,然后通过复制分配给我的那些内容到内存vector.此外,据我所知,移动语义在这里无济于事,因为它并不完全像是X指向位于其他地方的资源的指针.

我可以直接调用分配给我的原始内存块上的构造函数vector吗?如果没有,处理这类情况的正确方法是什么?

c++ memory-management stdvector

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

在Java中获得最适合的实例方法

如果我运行以下程序:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { new Object().getClass() };
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};
Run Code Online (Sandbox Code Playgroud)

我在命令行上得到以下内容:

true
Run Code Online (Sandbox Code Playgroud)

另一方面,如果我修改参数行一点:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { arg.getClass() }; // changed a little here so it's a bit more dynamic --
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};
Run Code Online (Sandbox Code Playgroud)

我明白了:

Exception in thread "main" …
Run Code Online (Sandbox Code Playgroud)

java reflection methods

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

获取由typedefed指针指向的sizeof类型

我的代码如下所示:

#include <stdio.h>
typedef struct SomeStruct* ptr;
main(){
  printf("%lu\n",sizeof(ptr));
}
Run Code Online (Sandbox Code Playgroud)

给定的代码将打印出大小struct SomeStruct*.但是我想要sizeof(struct SomeStruct).如果我事先不知道结构的名称,有没有办法让我仍然找到指向的结构的大小ptr

c sizeof

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

在 Python Tkinter 中绑定 Ctrl (-)

我有一个带有文本小部件的应用程序,我希望当用户按下 Control 键和减号键 (-) 时我的字体变小。

编码

self.bind_all("<Control-=>", self.increaseFont)
Run Code Online (Sandbox Code Playgroud)

当用户按下控制键和等号键时,似乎可以很好地使字体增加,但行

self.bind_all("<Control-->", self.decreaseFont)
Run Code Online (Sandbox Code Playgroud)

似乎不起作用。

当我尝试运行它时,它给了我一个运行时错误:

Traceback (most recent call last):
  File "./mathEditor.py", line 122, in <module>
    app = MathEditor(fileName = sys.argv[1])
  File "./mathEditor.py", line 40, in __init__
    self.bind_all("<Control-->", self.decreaseFont)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 997, in bind_all
    return self._bind(('bind', 'all'), sequence, func, add, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 940, in _bind
    self.tk.call(what + (sequence, cmd))
_tkinter.TclError: no event type or button # or keysym
Run Code Online (Sandbox Code Playgroud)

python tkinter

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

@(at 符号)在 Swift 中做什么?

当我在 Xcode 中创建一个新的 swift iOS 项目时,我看到

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Run Code Online (Sandbox Code Playgroud)

由于谷歌搜索的工作方式,搜索“@”符号的编程帮助似乎相当困难......

它是像Python一样应用装饰器的简写吗?即 UIApplicationMain 是一个接受单个类参数的函数,并且类是 Swift 中的第一类对象吗?

swift

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

声明一个C++ set迭代器

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?
c ++模板typename迭代器

以下代码将无法编译:

#include <iostream>
#include <set>
using namespace std;

template<class T>
void printSet(set<T> s){
    set<T>::iterator it;
}

int main(int argc, char** argv){
    set<int> s;
    printSet<int>(s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >)’:
set.cpp:7: error: expected `;' before ‘it’
set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >) [with T = int]’:
set.cpp:12:   instantiated from here
set.cpp:7: error: dependent-name ‘std::set<T,std::less<_Key>,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
set.cpp:7: note: say …
Run Code Online (Sandbox Code Playgroud)

c++ templates iterator compiler-errors

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

Python在字符串中的特定点重新匹配

如果我s在Python中有一个给定的字符串,是否可以轻松检查正则表达式是否匹配从字符串中特定位置开始i的字符串?

我宁愿不将整个字符串从i最后切片到最后,因为它看起来不太可扩展(re.match我认为排除了).

python regex

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