小编Kon*_*lph的帖子

什么是有条件地控制for循环方向的最佳方法

我的代码中有一个块,其中for循环应该根据条件向前或向后运行.

if (forwards) {
    for (unsigned x = 0; x < something.size(); x++ ) {
        // Lots of code
    }

} else {
    for (unsigned x = something.size()-1 ; x >= 0 ; x-- ) {
        // Lots of code
    }
} 
Run Code Online (Sandbox Code Playgroud)

有没有一个很好的方法来设置它,所以我不重复两次for循环中的所有代码?

有问题的'东西'是std :: vector <>,所以也许它可以用迭代器?(我没有使用C++ 11)

c++ iterator loops

6
推荐指数
3
解决办法
1594
查看次数

Windows中的多个shell命令

我正在尝试在R中复制一个shell命令,并且无法弄清楚如何将命令串在一起.这只返回工作文件夹的内容(system()由于某种原因失败):

> shell("dir")
 Volume info ..
 Directory of E:\Documents\R
 contents are listed..
Run Code Online (Sandbox Code Playgroud)

现在让我们尝试导航到C盘并运行dir(不使用明显的dir C:)..

> shell("cd C:")
C:\
> shell("dir")
 Volume in drive E is GT
 etc..
Run Code Online (Sandbox Code Playgroud)

所以似乎命令不能单独输入,因为shell不记得工作目录.所以..

> (cmd = "cd C:
+ dir")
[1] "cd C:\ndir"
> shell(cmd)
C:\
Run Code Online (Sandbox Code Playgroud)

没有运气,因为没有报告C:文件夹.我尝试过的其他方法也失败了.感谢任何想法.

r

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

osx - 获取pthread id

在system.log中我可以看到我的进程:

thread 515376 caught burning CPU! It used more than 50% CPU
Run Code Online (Sandbox Code Playgroud)

我使用多个线程,所以我尝试在线程使用的runnable方法中打印线程id,如下所示:

void* runnable1(void* ptr)
{
    pthread_t tid = pthread_self();
    printf("HELLO from thread runnable1 with id : %ld\n", tid);

    ...
}
Run Code Online (Sandbox Code Playgroud)

但是我得到这样的id:

HELLO from thread runnable1 with id : 4488212480
Run Code Online (Sandbox Code Playgroud)

与system.log中的不同.

问题是,如何以system.log中出现的方式获取线程ID?

macos pthreads

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

使用mkstemp()创建的文件被删除?

我有一个调用mkstemp()的程序,写了一些返回fd的东西,然后关闭fd.我希望文件保留,直到我自己删除它!使用rm命令等等.我的问题是:Linux会在关闭后删除此文件(fd)吗?

c linux temporary-files

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

从非对称矩阵得到上三角矩阵

有没有一种简单的方法来检索R中非对称矩阵的上(或下)三角矩阵?对于对称矩阵,这可以使用mat[upper.tri(mat)],但对于非对称矩阵怎么样?这里上三角矩阵的定义如下:如果具有超过1/2部分的单元属于由对角线界定的矩阵的右上角,则该单元属于上三角矩阵(例如, ,图中的红色部分).

谢谢. 在此输入图像描述

r matrix

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

“有效类型”的说明?

我正在阅读6.5表达式中规定的C规范

对象访问其存储值的有效类型是该对象的声明类型(如果有)。如果通过具有非字符类型的左值将值存储到没有声明类型的对象中,则左值的类型将成为该访问和不修改该访问的后续访问的对象的有效类型。储值。

谁能解释这意味着什么?我有一种模糊的感觉,它与指针和有关malloc(),但是这是我在没有律师帮助的情况下所能得到的。

根据答案更新:我可以安全地这样做吗?

struct point {
    int x;
    int y;
};

int main() {
    int *x = malloc(1000);
    *x = 10;
    printf("%d\n", *x);

    struct point *p = x;
    p->x = 5;
    p->y = 10;
    printf("%d %d\n", p->x, p->y);
}
Run Code Online (Sandbox Code Playgroud)

我收到警告,但对我而言有效。可以保证工作吗?

c

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

如何使用 Poetry 将 Python 包部署到 Gitlab 包注册表?

我正在尝试设置Poetry以将包部署到我们内部的 Gitlab 包注册表。根据在线的其他来源,存储库 ID 应该是https://gitlab.com/api/v4/projects/<project id>/packages/pypi,但无论我尝试什么,Poetry 都会返回

[上传
错误] HTTP 错误 404:未找到

任何人得到这个工作?

python gitlab python-poetry

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

如何正确运行 2 个同时等待事物的线程?

基本上,我有 2 个线程,接收和发送。我希望能够输入一条消息,每当我收到一条新消息时,它就会“打印在我正在输入的行上方”。首先我认为会起作用,您可以粘贴它,它将运行:

import multiprocessing
import time
from reprint import output
import time
import random
import sys

def receiveThread(queue):
    i = 0
    while True:
        queue.put(i)
        i+=1
        time.sleep(0.5)

def sendThread(queue):
    while True:
        a = sys.stdin.read(1)
        if (a != ""):
            queue.put(a)
        


if __name__ == "__main__":
    send_queue = multiprocessing.Queue()
    receive_queue = multiprocessing.Queue()

    send_thread = multiprocessing.Process(target=sendThread, args=[send_queue],)
    receive_thread = multiprocessing.Process(target=receiveThread, args=[receive_queue],)
    receive_thread.start()
    send_thread.start()

    with output(initial_len=2, interval=0) as output_lines:
        while True:
            output_lines[0] = "Received:  {}".format(str(receive_queue.get()))
            output_lines[1] = "Last Sent: {}".format(str(send_queue.get()))
Run Code Online (Sandbox Code Playgroud)

但是这里发生的是我无法发送数据。与我 put 时不同,输入不会给我一个 EOF a …

python input command-line-interface python-multiprocessing

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

如何根据数据框中以前的值制作一列

我有一个数据框:

user_id      url
111          google.com
111          youtube.com
111          youtube.com
111          google.com
111          stackoverflow.com
111          google.com
222          twitter.com
222          google.com
222          twitter.com
Run Code Online (Sandbox Code Playgroud)

我想创建一个列来显示之前访问过这个 URL 的事实。

期望的输出:

user_id      url                 target
111          google.com          0
111          youtube.com         0
111          youtube.com         1
111          google.com          1
111          stackoverflow.com   0
111          google.com          1
222          twitter.com         0
222          google.com          0
222          twitter.com         1
Run Code Online (Sandbox Code Playgroud)

我可以用循环来做到这一点,但它看起来不太好。可以用熊猫制作吗?

python pandas

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

如何选择除某些行和列之外的整个矩阵?

我有一个 5x4 矩阵,我想选择除第 2 到 4 行和第 2 到 3 列中的元素之外的所有元素(设置为 0)。基本上,沿矩阵“边缘”的所有元素都应设置为 0 . 目前,我的代码是

mat[ -(2:4), -(2:3) ] <- 0
Run Code Online (Sandbox Code Playgroud)

但是,这(取消)以 OR 方式选择元素,因此,只有矩阵的角设置为 0。如何以 AND 方式选择它们?

r matrix

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