小编ver*_*men的帖子

使用uniform_int_distribution与模数运算有什么好处?

根据以下结果,使用%操作在两个数字之间生成均匀的随机整数几乎比使用的快3倍std::uniform_int_distribution:是否有充分的理由使用std::uniform_int_distribution

码:

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <random>

#include <cstdio>
#include <cstdlib>

using namespace std;

#define N 100000000

int main()
{

clock_t tic,toc;

for(int trials=0; trials<3; trials++)
{
    cout<<"trial: "<<trials<<endl;

    // uniform_int_distribution
    {
        int res = 0;
        mt19937 gen(1);
        uniform_int_distribution<int> dist(0,999);

        tic = clock();
        for(int i=0; i<N; i++)
        {
            int r = dist(gen);
            res += r;
            res %= 1000;
        }
        toc = clock();
        cout << "uniform_int_distribution: "<<(float)(toc-tic)/CLOCKS_PER_SEC << endl;
        cout<<res<<" …
Run Code Online (Sandbox Code Playgroud)

c++ random stl c++11

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

Bash中的Perl:如何从管道中读取并同时将参数传递给perl?

以下是我正在尝试的代码:

echo "a b c" | perl -e 'print $ARGV[0]; print $ARGV[1]; print $_;' "abc" "def"
Run Code Online (Sandbox Code Playgroud)

此代码的输出是:

abcdef
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么"打印$ _;" 不像往常一样打印"ab c".有任何想法吗?

bash perl arguments pipe

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

在perl中增加特定位置的整数

我有一个像这样的字符串:

1,2,4 0:5 1:10 3:14
Run Code Online (Sandbox Code Playgroud)

我要转换成

1,2,4 1:5 2:10 4:14
Run Code Online (Sandbox Code Playgroud)

只有":"之前的数字必须加1.

我试过了:

perl -w -e '$s="1,2,4 0:5 1:10 3:14"; 
$s =~ s/([0-9]*):/print(($1+1).":")/ge; 
print("$s\n");'
Run Code Online (Sandbox Code Playgroud)

奇怪的回归

1:2:4:1,2,4 15 110 114
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法来实现我的目标?

regex perl increment

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

如何在Ubuntu中一次安装所有perl模块

我想在Ubuntu中一次安装所有可用的perl模块.这样的事情可能吗?

installation ubuntu perl perl-module

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

perl inside bash:如何在保存在字符串中的脚本上调用perl

我需要在不同的文件上多次执行相同的perl脚本.

为了简化这个过程,我试图将perl脚本保存为bash字符串,并在字符串上调用perl,如下面代码的"不工作"部分:

#!/bin/sh

# works
perl -e 'print 1;'

# doesn't work
S="'print 1;'"
perl -e $S
perl -e $S
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

1Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我能以其他方式达到同样的效果吗?

bash perl sh

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

Armadillo中的稀疏svd(C++)

根据http://arma.sourceforge.net/docs.html#part_c,Armadillo支持以下功能:

eig_sym
eig_gen
eigs_sym
eigs_gen
svd
svd_econ
Run Code Online (Sandbox Code Playgroud)

但似乎没有像"svds_econ"这样的函数,它在"稀疏"矩阵上运行并返回奇异值和向量.

有没有办法在Armadillo中实现这个功能?

c++ eigenvalue svd armadillo

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

标签 统计

perl ×4

bash ×2

c++ ×2

arguments ×1

armadillo ×1

c++11 ×1

eigenvalue ×1

increment ×1

installation ×1

perl-module ×1

pipe ×1

random ×1

regex ×1

sh ×1

stl ×1

svd ×1

ubuntu ×1