小编Nev*_* V.的帖子

C ++:用于订购元素的struct vs函数

我有struct两个领域:

struct road {
    int from, len ;
};
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我需要能够订购roads:

  • 通过升序from排列

  • 通过len在优先级队列中升序

因此,我包括:

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
Run Code Online (Sandbox Code Playgroud)

我遇到过一些网站建议重载operator<,但是由于这两种可能的顺序感觉不对,只能解决这两种情况之一。

通过弄乱教科书,我可以使它起作用:

bool cmpFrom (const road & a, const road & b) {
    return (a.from < b.from) ;
}

struct cmpLen {
    bool operator () (const road & a, const road & b){
        return (a.len < b.len) ;
    }
};
Run Code Online (Sandbox Code Playgroud)

用于:

std::sort(trips, trips + nbRoads, &cmpFrom) ;
std::priority_queue<road, std::vector<road>, …
Run Code Online (Sandbox Code Playgroud)

c++ sorting struct priority-queue

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

来自 Python 的键盘中断不会中止 Rust 函数 (PyO3)

我有一个用 PyO3 用 Rust 编写的 Python 库,它涉及一些昂贵的计算(单个函数调用最多 10 分钟)。从 Python 调用时如何中止执行?

Ctrl+C 好像只有在执行结束后才会处理,所以本质上是没有用的。

最小可重现示例:

# Cargo.toml

[package]
name = "wait"
version = "0.0.0"
authors = []
edition = "2018"

[lib]
name = "wait"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.10.1"
features = ["extension-module"]
Run Code Online (Sandbox Code Playgroud)
# Cargo.toml

[package]
name = "wait"
version = "0.0.0"
authors = []
edition = "2018"

[lib]
name = "wait"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.10.1"
features = ["extension-module"]
Run Code Online (Sandbox Code Playgroud)
// src/lib.rs

use pyo3::wrap_pyfunction;

#[pyfunction]
pub fn sleep() …
Run Code Online (Sandbox Code Playgroud)

python keyboardinterrupt rust pyo3

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

为什么“ f”在Bash正则表达式中匹配c *?

我正在尝试使用bash样式正则表达式解析一个可选参数,每个字母都是一个选项。

option       regex        expected result
abc     =~   *a*    -->   match, a is on
abc     =~   *z*    -->   no match, z is off
Run Code Online (Sandbox Code Playgroud)

我无法确定为什么它无法正常工作,但是后来我发现这是Bash的行为:

$ [[ "f" =~ *c ]]; echo $?
2                                         # ok
$ [[ "f" =~ *c* ]]; echo $?
2                                         # ok
$ [[ "f" =~ c ]]; echo $?
1                                         # ok
$ [[ "f" =~ c* ]]; echo $?
0                                         # wtf ?
$ [[ "f" =~ f ]]; echo $?
0                                         # ok
$ …
Run Code Online (Sandbox Code Playgroud)

regex bash

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

Crystal 编译器未检测到该对象不是 nil

我有以下课程:

class X
    property son, val
    def initialize(@val : Int32)
        @son = nil.as X?
    end

    def add(other : X?)
        unless other.nil?
            if @son.nil?
                @son = other
            else
                @son.add(other)
            end
        end
    end
end

x = X.new 5
x.add(nil)
x.add(X.new 3)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试build我得到

Showing last frame. Use --error-trace for full trace.

In nil-test.cr:12:22

 12 | @son.add(other)
           ^------
Error: undefined method 'include' for Nil (compile-time type is (X | Nil))
Run Code Online (Sandbox Code Playgroud)

根据手册,这正是编译器应该识别出@son不能nilelse分支中的情况,但它显然没有这样做。

我究竟做错了什么 ?

注意:使用 …

crystal-lang

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