小编The*_*Pen的帖子

JS fetch 检查文件是否存在而不下载内容

如何使用 JS fetch API 检查 URL 上是否存在文件,而不下载该文件(如果存在)?本质上,我只想检索状态代码而不是内容。

我不想下载该文件,因为它可能很大,并且下载整个文件会浪费带宽,因为我不关心内容。

javascript http http-status-codes fetch

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

使用概率分布展开整数的随机范围

我试图解决生成1到7之间随机整数的经典问题,给出一个生成1到5之间随机整数的函数.我的方法是将2个调用的结果添加到rand5(),有效地将其转换为"掷骰子的总和"问题.和骰子滚动的概率相当容易计算,所以我在这里使用它.对此的解释是在代码之后

我的问题是:我如何计算计数器的值应该是多少?通过实验验证,当前值不正确.是否存在满足概率的整数值?使用这种方法有没有更好的方法来解决这个问题?

def rand5():
    return random.randint(1,5)

def rand7():
    counter = [1,2,3,4,5,4,3]
    while 0 not in counter:
        sum = rand5() + rand5() - 2
        if sum <= 6:
            counter[sum] -= 1
    return counter.index(0) + 1
Run Code Online (Sandbox Code Playgroud)

作为参考,以下代码似乎创建随机分布.

test_counter = [0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(500000):
    test_counter[rand5() + rand5() - 2] += 1

test_counter[0] *= 60
test_counter[1] *= 30
test_counter[2] *= 20
test_counter[3] *= 15
test_counter[4] *= 12
test_counter[5] *= 15
test_counter[6] *= 20
test_counter[7] *= 0
test_counter[8] *= 0

print(test_counter)
Run Code Online (Sandbox Code Playgroud)

概率说明:可以通过列出骰子的可能组合来计算掷骰子的概率.对于这个问题,每个die(rand5函数)生成的数字将是: …

python random integer probability dice

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

VSCode“转到定义”打开新选项卡而不是使用现有选项卡

我在单独的分割窗格中打开了 2 个文件,A和。B中的代码A调用 中定义的函数BB当我转到函数的定义时, VSCode没有切换到包含 的窗格,而是B在同一窗格中再次打开。如何让它切换到现有的打开文件?

visual-studio-code

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

C++ 条件表达式在其他类型为 throw 表达式时删除引用

我有一个引用T*:的函数void f(T *&t);。当我使用带有 throw, 的条件表达式调用它时f(t == nullptr ? throw "nullptr" : t),程序无法编译:

error: cannot bind non-const lvalue reference of type 'T*&' to an rvalue of type 'T*'
note:   initializing argument 1 of 'f(T*&)'
Run Code Online (Sandbox Code Playgroud)

然而,用编译替换上面的调用f(t)就可以了。

这是为什么?整个表达式应与非抛出操作数具有相同的类型: https: //en.cppreference.com/w/cpp/language/operator_other。从链接中,Either E2 or E3 (but not both) is a (possibly parenthesized) throw-expression. The result of the conditional operator has the type and the value category of the other expression.

可重现的示例: https: //godbolt.org/z/9MW1Kevxz

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ reference rvalue lvalue

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

Rust 将 String 上的迭代器转换为 &amp;[&amp;str]

如何将返回字符串的迭代器转换为可以传递到需要的函数中的东西&[&str]?我试图将迭代器映射到&strs 上的迭代器,但出现错误:

error[E0515]: cannot return reference to function parameter `s`
  --> src/main.rs:89:52
   |
89 |     let args: Vec<&str> = std::env::args().map(|s| s.as_str()).collect();
   |                                                    ^^^^^^^^^^ returns a reference to data owned by the current function
Run Code Online (Sandbox Code Playgroud)

为什么会出现这个错误?args是 main 中的局部变量,因此它的生命周期肯定会延伸到函数末尾,因此在整个parse_args调用过程中都处于活动状态。

如果函数应该采用不同的类型,我可以更改它,但我的印象是这种类型对于我的目的来说是正确的 - 我不需要对向量的拥有访问权限,也不需要对值的拥有访问权限。

这是重现错误的代码:

fn main() {
    let args: Vec<&str> = std::env::args().map(|s| s.as_str()).collect();
    let config = parse_args(&args);
    // do stuff with config
}


fn parse_args(args: &[&str]) {
// parse args
}
Run Code Online (Sandbox Code Playgroud)

string iterator slice rust

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