小编pod*_*adu的帖子

如何在numpy Python中查找一维二进制数组的十进制值

我有一个像这样的布尔 numpy 数组,

>>> np_arr
array([[1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 1, 0, 0],
       [1, 1, 1, 1, 0, 1, 0, 0],
       [1, 1, 1, 1, 0, 1, 0, 0]])
Run Code Online (Sandbox Code Playgroud)

和另一个像这样的一维数组,

>>> another_arr
array([128,  64,  32,  16,   8,   4,   2,   1]) …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy python-3.x

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

使用C++中的零初始化一个向量数组

我想要一个向量数组,其中0作为所有单个向量中的单个元素.有更有效的方法吗?这有用吗?

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){

        int lastAnswer = 0;
        int n,q;
        cin >> n >> q;
        vector<int> seqArr[n];
        for(int i=0;i<n;i++)
        {
                fill(seqArr[i].begin(),seqArr[i].end(),0);
        }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

指向c中指针赋值的指针

为什么这段代码不起作用?

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv){

    char * new;
    new = malloc(10);
    const char * s1 = "hello";

    while(*s1){
        *new++ = *s1++;
    }
    printf("%s",new);

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

我运行了gdb,发现*s1没有分配给*new.为什么?

c pointers

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

使用完整缓冲区的生产者-消费者算法

我正在阅读 Galvin OS 关于生产者消费者问题的书,并浏览了这段代码。

\n\n

全局定义

\n\n
#define BUFFER_SIZE 10\ntypedef struct {\n    . . .\n} item;\n\nint in  = 0;\nint out = 0;\n
Run Code Online (Sandbox Code Playgroud)\n\n

制片人

\n\n
while (((in + 1) % BUFFER_SIZE) == out)\n    ; /* do nothing */\nbuffer[in] = next_produced;\nin = (in + 1) % BUFFER_SIZE ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

消费者

\n\n
while (in == out)\n    ; /* do nothing */\nnext_consumed = buffer[out];\nout = (out + 1) % BUFFER_SIZE;\n
Run Code Online (Sandbox Code Playgroud)\n\n

这就是加尔文书中所说的:

\n\n
\n

此方案最多允许缓冲区中同时存在 BUFFER_SIZE \xe2\x88\x92 1 个项目。我们将其作为练习,让您提供一个解决方案,\n BUFFER_SIZE 项可以同时位于缓冲区中。

\n
\n\n …

c algorithm producer-consumer

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

标签 统计

c ×2

algorithm ×1

arrays ×1

c++ ×1

c++11 ×1

numpy ×1

pointers ×1

producer-consumer ×1

python ×1

python-3.x ×1