小编Kon*_*lph的帖子

如何查找数组中的元素个数?

int a[10];
for(int i=0;i<5;i++)
{
   a[i]=i;
}
int len=sizeof(a)/sizeof(int);
print("%d",len);
Run Code Online (Sandbox Code Playgroud)

上面的代码打印10,但实际存在的元素数是5。需要帮助。

如果它是一个字符数组,这不会是一个问题,但这里的整数数组会引起痛苦。

c arrays

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

如何使用指针编写具有可变数量参数的函数?

所以我有一个任务是使用指针编写一个带有可变数量参数的函数“mult”。而这个函数必须计算浮点数的乘积。

我遵循了我们的大学给我们的指南,但我的产品仍然为零。我发现问题是要相乘的每个其他数字都是零。

#include <iostream>


using namespace  std;

int mult(int k,...){
    int* p = &k;
    int m = 1;
    for(; k != 0; k--){
        m *= *(++p);
    }
    return m;
}

int main(){
    float res1 = mult(11,45,10,9,8,7,6,5,4,3,2,2);
    float res2 = mult(7,12,23,0.3,0.6,1,2);
    float res3 = mult(3,0.6,-12,-0.9);
    cout << "Your results are:\n"
         <<res1<<"\n"
         <<res2<<"\n"
         <<res3<<"\n";

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

样本输出

以下是指南中的示例:

void Print_stor(int k, ...)
{
 int n=k;
 int a[n];
 int *p = &k;
 for ( ; k!=0;k--)
 a[k-1]=*(++p);
 for(int i=n-1; i>=0; i--)
 printf("%i ", …
Run Code Online (Sandbox Code Playgroud)

c++ variables arguments function

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

为什么我的无效模式没有抛出 PatternSyntaxException?

我想测试一个正则表达式在 Java 1.8.0_241 中是否有效

public static boolean isRegExpValid(String regExp) {
    try {
        Pattern.compile(regExp);
        return true;
    } catch (PatternSyntaxException e) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我正在测试三位数的正确正则表达式和不正确的正则表达式。

@Test
public void testValidRegexp() {
    assertTrue(isRegExpValid("\\d{3}"));
}

@Test
public void testInvalidRegexp() {
    assertFalse(isRegExpValid("{3}"));
}
Run Code Online (Sandbox Code Playgroud)

为什么我的第二次测试testInvalidRegexp失败了?isRegExpValid("{3}")应该返回false,但返回true。

在 Javascript 中,{3}正确失败并显示Nothing to repeat异常。 在此处输入图片说明

java regex unit-testing patternsyntaxexception

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

Curve25519公钥是309字节,私钥是587,不应该是32字节吗?

我正在尝试使用 bouncycastle 在我的 java 程序中实现 curve25519,这是我想出的代码:

package crypto;

import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;

public class Curve {

    public KeyPair generateKeyPair() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
        X9ECParameters ecP = CustomNamedCurves.getByName("curve25519");
        ECParameterSpec ecSpec = new ECParameterSpec(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
        keyGen.initialize(ecSpec);
        return keyGen.generateKeyPair();
    }

}
Run Code Online (Sandbox Code Playgroud)

而我的主要方法:

package crypto;

import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Test_Curve {

    public static …
Run Code Online (Sandbox Code Playgroud)

java bouncycastle diffie-hellman public-key

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

std:hash 可以访问类的私有成员

我想散列一个具有两个私有成员的类,例如:

foo.h

class Foo {
    private:
        std::string a;
        std::string b;

    public:
        Foo (std::string a, std::string b);
        bool operator==(const Foo& other) const;
        bool operator!=(const Foo& other) const;
        std::size_t operator()(const Foo& ) const;
};

namespace std {
    template <> struct hash<Foo> {
        std::size_t operator()(const Foo& cp) const;
    };
}
Run Code Online (Sandbox Code Playgroud)

文件

Foo::Foo (std::string _a, std::string _b) {
    this->a = _a;
    this->b = _b;
}

bool Foo::operator== (const Foo& other) const {
    return this->a == other.a && this->b == other.b;
}

bool Foo::operator!= (const …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap stdset c++11 stdhash

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

如何避免我非常大的嵌套 for 循环?

我必须使用物理公式来实现一些模拟。在一个公式中,有许多变量。我想用 100 个样本改变这些变量。对于每个样本,我必须使用所有组合进行计算。简化的 for 循环更好地解释了我想要做什么:

set.seed(3)
a = rnorm(100)
b = rnorm(100)
c = rnorm(100)
d = rnorm(100)
f = rnorm(100)

for (a in 1:length(a)) {
        
        for (b in 1:length(b)) {
                
                for (c in 1:length(c)) {
                        
                        for (d in 1:length(d)) {
                                
                                for (f in 1:length(f)) {
                                        
                                        value = a + b / c * d - f # for illustrative purposes only
                                        # .... 
                                        # ... then I append the value to a vector etc.
                                        
                                }
                                
                        }
                        
                }
                
        }
        
} …
Run Code Online (Sandbox Code Playgroud)

loops for-loop r nested-loops

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

使用与类型参数匹配的 Scanner.nextX 读取类型参数值列表

与此相关的帖子太多了,我认为参考它们无济于事,但是如果人们发现特别有用的帖子,将它们添加到这里会很棒。

  ArrayList<Integer> readIntegersFrom(Scanner scnr) {
      ArrayList<Integer> lst = new ArrayList<Integer>();
      while (scnr.hasNext()) {
         lst.add(scnr.nextInt());
      }
      return lst;
   }
Run Code Online (Sandbox Code Playgroud)

整数出现在 4 个地方:

  1. 方法的名称
  2. ArrayList 返回类型的 Type 参数
  3. lst 类型的 Type 参数
  4. 创建新列表的 Type 参数

到现在为止还挺好。但是,我的程序将这个方法复制了四次(我敢肯定,还会有更多),每个都有不同的类型参数和 Scanner 方法。(例如,ArrayList<Double>scnr.nextDouble()。)

我不怀疑我混淆了我使用过的许多其他语言的想法和技术,但是有什么方法可以概括这种方法,所以我可以告诉它我想要一个 ArrayList,比如说,Double,它应该使用 Scanner .nextDouble()?这个问题的两个部分是 (a) 传达 ArrayList 的类型参数和 (b) 传达要使用的 Scanner 方法。

我会满足于指定或推导类型参数的单一方法,并有一个枚举参数来告诉扫描器使用哪种方法,尽管这两个方法是直接连接的。

java generics method-parameters

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

取消引用 char 指针返回 int ?为什么?

例如,以下代码在编译时返回错误和警告int,更改为%d

警告:格式%s需要 type 的char *参数,但参数 2 具有类型int

void stringd() {
    char *s = "Hello";
    printf("derefernced s is %s", *s);
}
Run Code Online (Sandbox Code Playgroud)

c

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

无法在 R 的 dplyr 包中使用“intersect()”中的管道

语境

我想使用intersect()两个字符向量,并且我可以使用 立即执行此操作intersect(names(mtcars), a)。但是当我使用管道时出现错误。

问题

如何在intersect()R 中使用 dplyr 包中的管道。

可重现的代码

library(tidyverse)

a = c('mpg', 'cyl')

intersect(names(mtcars), a) # run correctly

mtcars %>% intersect(x = names(.), y = a) # error occur
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

即使使用基元,List&lt;int[]&gt; 也是正确的语句

在 Java 中,在集合的尖括号 <> 内,我们应该提供像整数、字符串、字符等泛型,我们不能给出基元,但这是正确的列表/集合初始化:

List<int[]> ls = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

基元数组可以定义为泛型吗?因为据我所知Java中<>都是用来定义泛型的。当我第一次看到这种初始化时,我感到惊讶和好奇,并且不相信。

java generics collections

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