小编Pet*_*etr的帖子

Java中的PriorityQueue如何排序重复条目?

这可能听起来很愚蠢,但是当你有(键,值)对的对象并且你根据键对它们进行排序时它是有意义的.用代码说明我的观点:

public class Pair implements Comparable<Pair> {
    private int value;
    private int key;

    public Pair(int key, int value) {
        this.key   = key;
        this.value = value;
    }

    @Override
    public int compareTo(Pair o) {
        if (this.key > o.key)
            return 1;
        else if (this.key < o.key)
            return -1;
        return 0;
    }
}

public class program {
    public static void main(String[] args) {
        PriorityQueue<Pair> queue = new PriorityQueue<Pair>;
        queue.add(new Pair(1,1));
        queue.add(new Pair(1,2));
        queue.add(new Pair(1,3));

        Pair pair = queue.poll(); // What would be in pair? …
Run Code Online (Sandbox Code Playgroud)

java priority-queue

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

可以锁定变量以防止在c ++中对其进行更改吗?

我正在使用一个成员变量,并且在程序的某个时刻我想要更改它,但我更喜欢在其他地方"锁定"以防止意外更改.

代码解释:

class myClass {
    int x;  // This should be prevented to being changed most of the time
    int y;  // Regular variable
    myclass() {x = 1;}
    void foo1 () {x++; y++;} // This can change x
    void foo2 () {x--; y--;} // This shouldn't be able to change x
                             // I want it to throw a compile error
};
Run Code Online (Sandbox Code Playgroud)

问题是:能以某种方式实现吗?像永久const_cast这样的东西?

我知道我可以立即使用构造函数初始化列表和常量,但我需要稍后更改我的变量.

c++ variables const

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

如何使用OpenSSL生成包括通用(任意)扩展名的证书请求?

我已经在网上搜索了几个小时,但找不到解决方法。我已经能够使用以下命令创建自签名CA证书:

openssl genrsa -out ca.key 1024
openssl req -new -x509 -extensions v3_ca -key ca.key -out ca.crt -days 3650
Run Code Online (Sandbox Code Playgroud)

现在,我想创建新证书并用我的CA对其进行签名。在新证书中,我想拥有自己的扩展名-我们可以将其称为“ abc”,以使其具有整数值“ 1”。我尝试了以下命令:

openssl req -new -nodes -newkey rsa -extensions abc -keyout mycert.key -out mycsr.csr -days 365 -config ./openssl.cnf
Run Code Online (Sandbox Code Playgroud)

使用openssl.cnf仅包括以下内容的文件时:

[ abc ]

abc = ASN1:INTEGER:1
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Error Loading extension section abc
3073632456:error:0D06407A:asn1 encoding routines:a2d_ASN1_OBJECT:first num too large:a_object.c:109:
3073632456:error:22074073:X509 V3 routines:V3_GENERIC_EXTENSION:extension name error:v3_conf.c:271:name=abc
Run Code Online (Sandbox Code Playgroud)

我在SO上找到了这个模糊相关的topis,但它根本没有帮助...

似乎是什么问题?我该如何纠正自己?任何想法表示赞赏。

openssl asn.1 x509

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

如何在 Cobra 中的子命令上调用 SetOut()?

我正在尝试测试用 Cobra 编写的 CLI 应用程序,特别是测试子命令是否正确写入 STDOUT。为此,我尝试将 STDOUT 的输出重定向到我的缓冲区。不幸的是,无论出于何种原因,SetOut() 函数在通过调用 Commands() 获得的子命令上的行为并不如预期。

如何在 Cobra 中的子命令上正确调用 SetOut()?

这是我的代码:

package cmd

import (
    "os"
    "testing"
    "bytes"
    "io/ioutil"
    "github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
    cmd := &cobra.Command{}
    cmd.AddCommand(NewChildCmd())
    return cmd
}

func NewChildCmd() *cobra.Command {
    cmd := &cobra.Command{
        Use:   "child",
        Run: func(cmd *cobra.Command, args []string) {
                os.Stdout.WriteString("TEST\n")
        },
    }
    return cmd
}

func TestChild(t *testing.T) {
    cmd := NewCmd()
    buffer := new(bytes.Buffer)

    subCommands := cmd.Commands()
    for i := range subCommands {
        subCommands[i].SetOut(buffer)
    }

    cmd.SetOut(buffer) …
Run Code Online (Sandbox Code Playgroud)

go cobra go-cobra

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

标签 统计

asn.1 ×1

c++ ×1

cobra ×1

const ×1

go ×1

go-cobra ×1

java ×1

openssl ×1

priority-queue ×1

variables ×1

x509 ×1