小编Joh*_*nck的帖子

设置种子提升::随机

我想通过使用不同的种子数重置随机序列.运行此测试代码时:

boost::mt19937 gener(1);
boost::normal_distribution<> normal(0,1);
boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > rng(gener, normal);
cout << rng() << endl;
cout << rng() << endl;
cout << rng() << endl;
gener.seed(2);
cout << rng() << endl;
cout << rng() << endl;
gener.seed(1);
cout << rng() << endl;
gener.seed(2);
cout << rng() << endl;
gener.seed(3);
cout << rng() << endl;
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

# seed(1) via constructor
-2.971829031
1.706951063
-0.430498971
# seed(2)
-2.282022417
-0.5887503675
# seed(1)
0.2504171986
# seed(2)
-0.5887503675
# seed(3)
0.2504171986
Run Code Online (Sandbox Code Playgroud)

显然我做错了.我怎么能克服这个问题?

c++ random boost seed

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

R dcast相当于python pandas

我试图在python中执行相当于以下命令:

test <- data.frame(convert_me=c('Convert1','Convert2','Convert3'),
                   values=rnorm(3,45, 12), age_col=c('23','33','44'))
test

library(reshape2)
t <- dcast(test, values ~ convert_me+age_col, length  )
t
Run Code Online (Sandbox Code Playgroud)

就是这样:

convert_me   values     age_col
Convert1     21.71502      23
Convert2     58.35506      33
Convert3     60.41639      44
Run Code Online (Sandbox Code Playgroud)

成为这个:

values     Convert2_33 Convert1_23 Convert3_44
21.71502          0           1           0
58.35506          1           0           0
60.41639          0           0           1
Run Code Online (Sandbox Code Playgroud)

我知道使用虚拟变量我可以得到列的值并转换为列的名称,但有没有办法轻松地合并它们(组合),就像R一样?

python r pandas

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

Boost Message Queue不是基于POSIX消息队列?无法选择(2)?

我以为我会使用Boost.Interprocess的Message Queue代替套接字在一个主机内进行通信.但是在深入研究之后,似乎这个库出于某种原因避开了POSIX消息队列工具(我的Linux系统支持),而是在POSIX共享内存之上实现.界面足够相似,你可能不会立即猜测,但似乎是这样.

我的缺点是获得的共享内存shm_open(3)似乎不可用select(2),而不是通过获得的POSIX消息队列mq_open(3).

在这种情况下,Boost的图书馆似乎失败了.有谁知道为什么这应该是?即使POSIX消息队列仅在某些系统上可用,我也希望Boost在可用的情况下使用该设施,并且只在必要时重新实现它.POSIX系统是否存在一些我尚未认识到的陷阱?

select boost epoll message-queue interprocess

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

列表2中的哪些数字大于和小于列表1中的每个数字

我正在使用python.我有两个列表,列表1是7000个整数长,列表2是25000个整数.我想通过列表1中的每个数字,找到列表2中最接近的数字,这个数字更大,最接近的数字小于列表1中的每个数字,然后计算列表2中这两个数字之间的差异.到目前为止我有:

for i in list1:
    for j in list 2:
        if list2[j]<list1[i]:
            a = max(list2)
        elif list2[j]>list1[i]:
            b = min(list2)
            interval = b-a
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.我想在列表2中找到小于列表1中特定数字的明确数字并知道最大值,然后找出列表2中比列表1中的数字更大的最小数字.有没有人有任何想法?谢谢

python search list

8
推荐指数
3
解决办法
1222
查看次数

R write(append = TRUE)覆盖文件内容

这是我的R代码:

out = file('testfile')
write('hello', file=out, append=T)
write('world', file=out, append=T)
close(out)
Run Code Online (Sandbox Code Playgroud)

当我运行它(使用R 3.1.0)时,testfile然后包含:

world
Run Code Online (Sandbox Code Playgroud)

我期望:

hello
world
Run Code Online (Sandbox Code Playgroud)

如果我使用cat()而不是使用相同的行为write().为什么?如何附加到文件?

file-io r append

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

节点本身可以​​提供静态文件而无需快递或任何其他模块..?

我是节点js领域的初学者.没有想法如何从url发送简单请求如下: - http:// localhost:9999/xyz/inde.html 我的文件层次结构是

server.js
xyz(folder)-
          |->index.html
Run Code Online (Sandbox Code Playgroud)

从我的服务器获取html页面.它在9999后运行

var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
}
http.createServer(onRequest).listen(9999);
console.log("Server has started.");
Run Code Online (Sandbox Code Playgroud)

我知道我可以从节点js服务器发送字符串(有html模板)并发送它作为响应,但如何发送文件没有快递和任何其他外部模块.谢谢

node.js

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

为什么这段代码片段在C和C++中产生完全不同的汇编代码?

struct v {
    int val[16];
};

struct v test(struct v a, struct v b) {
    struct v res;

    for (int i = 0; i < 16; i++)
        res.val[i] = a.val[i] + b.val[i];

    return res;
}
Run Code Online (Sandbox Code Playgroud)

编译为C++,GCC 7.2发出:

  push r10
  vmovdqu32 zmm0, ZMMWORD PTR [rsp+16]
  mov rax, rdi
  vpaddd zmm0, zmm0, ZMMWORD PTR [rsp+80]
  lea r10, [rsp+16]
  vmovdqu32 ZMMWORD PTR [rdi], zmm0
  pop r10
Run Code Online (Sandbox Code Playgroud)

编译为C:

  lea r10, [rsp+8]
  and rsp, -64
  mov rax, rdi
  push QWORD PTR [r10-8]
  push rbp
  mov …
Run Code Online (Sandbox Code Playgroud)

c c++ assembly gcc simd

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

除非返回值具有名称,否则为什么GCC无法优化?

考虑以下代码:

#include <array>

class C
{
    std::array<char, 7> a{};
    int b{};
};

C slow()
{
    return {};
}

C fast()
{
    C c;
    return c;
}
Run Code Online (Sandbox Code Playgroud)

GCC 6到9产生了非常肿的代码slow()

slow():
        xor     eax, eax
        mov     DWORD PTR [rsp-25], 0
        mov     BYTE PTR [rsp-21], 0
        mov     edx, DWORD PTR [rsp-24]
        mov     DWORD PTR [rsp-32], 0
        mov     WORD PTR [rsp-28], ax
        mov     BYTE PTR [rsp-26], 0
        mov     rax, QWORD PTR [rsp-32]
        ret
fast():
        xor     eax, eax
        xor     edx, edx
        ret
Run Code Online (Sandbox Code Playgroud)

两种功能在含义上有区别吗?Clang会发出fast() …

c++ optimization gcc g++

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

在作为静态成员包含在另一个类中的类的构造函数中使用 cout

以下代码

#include <iostream>

struct A {
    A() {
        std::cout << std::endl;
    }
};

struct B {
    static inline A a;
};

int main() {
}
Run Code Online (Sandbox Code Playgroud)

用gcc 编译后成功,但用clang 编译后因分段错误而崩溃。代码不标准还是叮当错了?

https://godbolt.org/z/tEvfrW

c++ clang initialization-order language-lawyer

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

用户定义的文字字符串:编译时长度检查

我有一个用户定义的文字运算符,它只对特定长度的字符串有意义,如下所示:

constexpr uint16_t operator "" _int(const char* s, std::size_t len)
{
    return len == 2 ? s[0] | (s[1] << 8) : throw;
}
Run Code Online (Sandbox Code Playgroud)

这有效:

"AB"_int // equals 16961
Run Code Online (Sandbox Code Playgroud)

但这也可以编译,我不希望它:

"ABC"_int // throws at runtime
Run Code Online (Sandbox Code Playgroud)

我试过了static_assert(len == 2),但在 constexpr 函数中是不允许的。

如何"ABC"_int在编译时导致错误?

c++ static-assert compile-time-constant user-defined-literals c++11

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