小编phi*_*sky的帖子

使用sed在没有已知分隔符的情况下在行中提取多个匹配项

我有一个大文本文件,其中包含嵌入句子中的概率.我想只提取那些概率和它们之前的文本.例

输入:

not interesting
foo is 1 in 1,200 and test is 1 in 3.4 not interesting
something else is 1 in 2.5, things are 1 in 10
also not interesting
Run Code Online (Sandbox Code Playgroud)

通缉输出:

foo is 1/1,200
and test is 1/3.4
something else is 1/2.5,
things are 1/10
Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的:

$ sed -nr ':a s|(.*) 1 in ([0-9.,]+)|\1 1/\2\n|;tx;by; :x h;ba; :y g;/^$/d; p' input

foo is 1/1,200
 and test is 1/3.4
 not interesting
something else is 1/2.5,
 things are 1/10

something else …
Run Code Online (Sandbox Code Playgroud)

regex sed

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

用基于范围的for循环替换多维for循环

我有一个Vec3课程.更换循环的最佳方法是什么?

for (int x = 20; x < 25; x++)
    for (int y = 40; y < 45; y++)
        for (int z = 2; z < 4; z++) doStuff({x,y,z});
Run Code Online (Sandbox Code Playgroud)

用这样的东西:

for(Vec3 v: Vec3range({20,40,2}, {25,45,4}))
    doStuff(v);
Run Code Online (Sandbox Code Playgroud)

没有任何运行时成本?

c++ for-loop c++11

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

编译的 gcc 和 tcc 结构的兼容性

我正在尝试从 C++ 运行 libtcc 以使用 C 作为运行时脚本语言。运行时编译的代码必须能够运行外部代码中的函数。当传递整数时,这工作得很好,但是当将结构从 tcc 代码传递到 gcc 代码时,会发生奇怪的事情。

最小运行示例:

#include <libtcc.h>
#include <stdio.h>
struct Vec {
    int x;
};
void tmp(struct Vec test) {
    printf("got %x\n",test.x);
}
int main() {
    TCCState* tcc; tcc = tcc_new();
    tcc_set_output_type(tcc, TCC_OUTPUT_MEMORY);
    tcc_add_symbol(tcc, "tmp", (void*)&tmp);
    tcc_compile_string(tcc, "\
        struct Vec {int x;};\
        void tmp(struct Vec test);\
        void fun() {\
            struct Vec x = {0};\
            tmp(x);\
        }");
    tcc_relocate(tcc, TCC_RELOCATE_AUTO);
    void (*fun)(void) = (void(*)())tcc_get_symbol(tcc, "fun");
    fun();
}
Run Code Online (Sandbox Code Playgroud)

运行:

gcc -ltcc -ldl test.c && ./a.out
> …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc struct tcc

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

标签 统计

c++ ×2

c ×1

c++11 ×1

for-loop ×1

gcc ×1

regex ×1

sed ×1

struct ×1

tcc ×1