小编dzh*_*lil的帖子

RAII和任务

我为sqlite3连接创建了以下类:

class SqliteConnection
{
public:
    sqlite3* native;

    SqliteConnection (std::string path){
        sqlite3_open_v2 (path.c_str(), &native, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    }

    ~SqliteConnection (){
        sqlite3_close(native);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后可以按如下方式初始化连接

SqliteConnection conn("./database.db");
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够共享此连接,将其存储为类中的成员等,并且问题在于默认赋值运算符operator=.做点什么

SqliteConnection conn("./database.db");
SqliteConnection conn1 = conn;
Run Code Online (Sandbox Code Playgroud)

当每个变量超出范围时,会导致对数据库指针进行两次sqlite3_close调用.当您需要将资源分配给不同的变量时,如何克服RAII的这一困难?

c++ raii assignment-operator

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

使用C++向量而不是C数组的性能损失

使用C++中的标准库中的向量而不是C中的数组会有性能损失吗?

c++ arrays vector std

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

修复函数的奇点

假设你有一个像这样的功能

F = lambda x: sin(x)/x
Run Code Online (Sandbox Code Playgroud)

评估F(0.0)会导致零除警告,并且不会给出预期的结果1.0.是否有可能编写另一个函数fix_singularity,当应用于上述函数时,它将提供所需的结果,这样就可以了

fix_singularity(F)(0.0) == 1.0
Run Code Online (Sandbox Code Playgroud)

或者正式fix_singularity通过以下测试:

import numpy as np

def test_fix_singularity():

    F = lambda x: np.sin(x)/x

    x = np.array((0.0, pi))

    np.testing.assert_array_almost_equal( F(x), [nan, 0] )

    np.testing.assert_array_almost_equal( fix_singularity(F)(x), [1, 0] )
Run Code Online (Sandbox Code Playgroud)

一种可能的实现是

def fix_singularity(F):
    """ Fix the singularity of function F(x) """

    def L(x):
        f = F(x)
        i = np.isnan(f)
        f[i] = F(x[i] + 1e-16)
        return f

    return L
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?

编辑:另外我如何抑制警告:

Warning: invalid value encountered in divide
Run Code Online (Sandbox Code Playgroud)

python numpy nan singular

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

在python终端中导航

我想在我的Mac上使用64位python解释器,所以我不得不从源代码重建.但是,使用我自己的自定义构建解释器时,当我从shell内部运行解释器时尝试导航时会遇到问题.将python键入bash shell会导致熟悉:

Python 2.6.3 (r263:75183, Oct 23 2009, 14:23:25) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用左箭头和右箭头导航时,我会得到奇怪的字符:

Python 2.6.3 (r263:75183, Oct 23 2009, 14:23:25) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^[[A^[[A^[[A^[[D^[[C^[[C^[[A^[[B^[[D^[[C
Run Code Online (Sandbox Code Playgroud)

这在Apple的默认解释器中不会发生.

是什么造成的?我该如何解决?

python navigation shell

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

标签 统计

c++ ×2

python ×2

arrays ×1

assignment-operator ×1

nan ×1

navigation ×1

numpy ×1

raii ×1

shell ×1

singular ×1

std ×1

vector ×1